可以求得成员函数,成员变量的地址
也可以得到普通变量和函数的地址或者值
换句话,就是又一个版本的AnythingToLong
7 m* S r/ s2 X优化过后,毫无性能损失
K* H( `* r( D3 }所以我觉得是一个较好的解决办法
// test.cpp : Defines the entry point for the console application.
//
! h% s% N9 i% e: f- ^3 `: {3 ? C% Q
#include "stdafx.h"
d- I0 g+ Y+ f
template<class T>
inline unsigned long ValueOf(const T &value)
$ N, [2 y% {0 d& f. G{
return *(unsigned long*)(&value);
( I: R0 H+ g( ~}
) f9 Z/ H5 U9 J9 e/ W- g" p6 \( o& T2 v+ ^, c3 \
template<class T>
: o- \$ N7 h7 Rinline int SizeOfArray(const T &array)
{
return sizeof(array)/sizeof(array[0]);
2 Q# e1 ^+ l% M/ N}
% |9 n( I7 Y3 b* D% b
class Test
9 K0 H8 u A7 I$ n4 S{
, W; ~8 s( D" P4 \, D& |# j- ?" jpublic:
& Z0 `+ D7 Q0 {- Qint a,b;
void f(){}
7 l1 M5 N5 c2 K4 j};
. B2 a3 `$ _5 v$ _: O
int main(int argc, char* argv[])
' V3 l% [5 G) @* s+ P$ `% E{
& u4 F1 ^; z, j) c) T+ D7 Aint t[16];
# j2 M! Y& \7 `int n=1000;
% O* h, W- T* f2 Z2 P/ F$ }
// cout<<SizeOfArray(t)<<endl;
cout<<ValueOf(&Test::a)<<endl;
cout<<ValueOf(&Test::b)<<endl;
- [0 H. F' q/ s" H& W9 Z* R9 m2 Tcout<<ValueOf(&Test::f)<<endl;
cout<<ValueOf(&t)<<endl;
cout<<(long)&t<<endl;
; a3 a9 N) A# H c, y3 K$ W1 L
cout<<ValueOf(n)<<endl;
: b0 V' L5 T, ~8 J [2 B# \! d( v7 N9 e! b
return 0;
3 ]6 e3 ^: }3 u$ R. a# B0 m" ?}
. D: c3 @ P" ~
//运行结果
7 ^1 i1 }, A( t
16
. ?2 g0 Z* d8 d' h0 H# p% W0
4
' s% j: r& p7 O4198425
. F* W- p' q7 q7 ?" N6684088
8 W! J2 P, b2 k; F+ Z6684088
- O+ R; T0 h- x% a( k/ }0 E! P1000
+ S, _; P' G& Z6 \Press any key to continue
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |