求得类成员地址的又一个方法
<P> </P><P>可以求得成员函数,成员变量的地址 </P>
<P>也可以得到普通变量和函数的地址或者值 </P>
<P>换句话,就是又一个版本的AnythingToLong </P>
<P>优化过后,毫无性能损失 </P>
<P>所以我觉得是一个较好的解决办法 </P>
<P> </P>
<P>// test.cpp : Defines the entry point for the console application. </P>
<P>// </P>
<P> </P>
<P>#include "stdafx.h" </P>
<P> </P>
<P>template<class T> </P>
<P>inline unsigned long ValueOf(const T &value) </P>
<P>{ </P>
<P> return *(unsigned long*)(&value); </P>
<P>} </P>
<P> </P>
<P>template<class T> </P>
<P>inline int SizeOfArray(const T &array) </P>
<P>{ </P>
<P> return sizeof(array)/sizeof(array); </P>
<P>} </P>
<P> </P>
<P>class Test </P>
<P>{ </P>
<P>public: </P>
<P> int a,b; </P>
<P> void f(){} </P>
<P>}; </P>
<P> </P>
<P>int main(int argc, char* argv[]) </P>
<P>{ </P>
<P> int t; </P>
<P> int n=1000; </P>
<P> </P>
<P> // cout<<SizeOfArray(t)<<endl; </P>
<P> cout<<ValueOf(&Test::a)<<endl; </P>
<P> cout<<ValueOf(&Test::b)<<endl; </P>
<P> cout<<ValueOf(&Test::f)<<endl; </P>
<P> </P>
<P> cout<<ValueOf(&t)<<endl; </P>
<P> cout<<(long)&t<<endl; </P>
<P> </P>
<P> cout<<ValueOf(n)<<endl; </P>
<P> </P>
<P> return 0; </P>
<P>} </P>
<P> </P>
<P>//运行结果 </P>
<P> </P>
<P>16 </P>
<P>0 </P>
<P>4 </P>
<P>4198425 </P>
<P>6684088 </P>
<P>6684088 </P>
<P>1000 </P>
<P>Press any key to continue </P>
页:
[1]