可以求得成员函数,成员变量的地址
也可以得到普通变量和函数的地址或者值
. e. v' B y& _: F& O9 Z换句话,就是又一个版本的AnythingToLong
! L( l& l/ R+ l. C优化过后,毫无性能损失
4 T& f7 f. Z5 i* M+ }2 U) b所以我觉得是一个较好的解决办法
// test.cpp : Defines the entry point for the console application.
//
) @, Q! d' {- o4 O6 p+ A
#include "stdafx.h"
' B. \+ e% t4 H g0 `+ B2 E0 a# m: l! q5 s# E W& x
template<class T>
inline unsigned long ValueOf(const T &value)
{
return *(unsigned long*)(&value);
% b3 ^- J# I; L3 C' v}
6 x2 i. }& R+ H+ r" K* d) _6 o4 U5 E7 w p
template<class T>
) Z0 B7 |8 j* O4 Sinline int SizeOfArray(const T &array)
2 |9 E4 `- l; m{
6 L) L6 e" K: A% }4 f( zreturn sizeof(array)/sizeof(array[0]);
}
- y! }$ r' g: P5 x7 _+ F1 Y. K a* [, k. M" Y5 O
class Test
{
8 Q, Z% u& t% s% m6 Npublic:
int a,b;
- j- g+ Q9 u! \! @6 D: o5 evoid f(){}
0 z4 \6 S/ J; H, n2 o* i6 Z};
+ b# c* {& G5 Z% x- S! d+ X( E( }
int main(int argc, char* argv[])
{
int t[16];
int n=1000;
// cout<<SizeOfArray(t)<<endl;
4 l$ }9 F# n' |2 W6 vcout<<ValueOf(&Test::a)<<endl;
cout<<ValueOf(&Test::b)<<endl;
1 d( K0 s* A6 k" gcout<<ValueOf(&Test::f)<<endl;
3 i- E- ]) [$ g( |$ ?5 Z
cout<<ValueOf(&t)<<endl;
cout<<(long)&t<<endl;
cout<<ValueOf(n)<<endl;
return 0;
}
: z! n% }' M7 ^1 E) O
//运行结果
) G4 B2 h4 x) P: B
16
0
6 M+ O; g& O3 t; e4
4198425
, q4 G4 T$ y, b# S6684088
6684088
) h4 w/ j: s5 G/ u4 V1000
Press any key to continue
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |