|
6 m' {4 p# R+ M5 v& _6 \4 F
可以求得成员函数,成员变量的地址 & x6 D" |9 q: Y& B
也可以得到普通变量和函数的地址或者值
9 F9 K2 W# m( y4 u3 t2 p$ H换句话,就是又一个版本的AnythingToLong
6 M* c) ~; D9 ]) I, b! Y3 b优化过后,毫无性能损失 * o. }/ O2 }: `; c! [0 F8 Q
所以我觉得是一个较好的解决办法
/ y9 q7 T. U6 i- b' e7 l
j. O) s$ ?: k X7 ]// test.cpp : Defines the entry point for the console application. $ S' r2 j0 m+ `& L
// 3 g" z1 q% C/ Z7 P2 i8 A7 ~
5 [" j2 a9 a$ n7 m7 f7 @8 ~. E
#include "stdafx.h" , h) a1 X( J$ G; K
( c b! Q% y' itemplate<class T>
, V" ?- f C* W/ q" a% l" }5 jinline unsigned long ValueOf(const T &value)
- q( D( m2 G2 ~4 s{ 7 F2 U- j. X2 m; Y8 J
return *(unsigned long*)(&value);
p. ?4 w& V& L, L' f3 y}
; r6 V9 k( f- y9 J 3 d, |$ U2 T% B
template<class T> & h6 @9 F: |' x4 S2 e. n S' `: }1 @
inline int SizeOfArray(const T &array)
, Z% v- \6 U1 R/ H& O{ * A0 }; b* p7 g/ V& p" @
return sizeof(array)/sizeof(array[0]);
& f9 {; C* O/ j& `1 u. H# e} 8 m" J8 m6 [4 b
# H9 O7 X/ \: G2 `/ _* m/ n
class Test
6 \' ~, D g. q) |# ]- j{ : W$ m" G ?9 {
public:
0 T% h% y; m5 \) S int a,b;
3 A: ~2 V3 p- D# K! J0 _$ V( U# f void f(){} 7 l8 J7 V" @9 V0 J: I5 T4 j
};
) ?# _0 R* g+ Z
( L! f* O% J. M% H, B% `int main(int argc, char* argv[])
0 l* T" D0 c! O, q{
& v# ^- O( F' S4 v# ]# G# W; U. j2 n int t[16]; / o% g+ A8 c+ k& J. d8 G4 V+ [
int n=1000; 2 g' } S! @8 a3 R* Z, X. l
5 ^( O9 d7 q7 P* B0 U2 O
// cout<<SizeOfArray(t)<<endl;
1 R' Z4 \9 j" l9 y9 C cout<<ValueOf(&Test::a)<<endl;
: t0 a, e1 w" [- [& s& ? cout<<ValueOf(&Test::b)<<endl; % O( |( t' @! j! K. @
cout<<ValueOf(&Test::f)<<endl;
: @8 N. M4 g- h, S) `5 J 2 h, e3 H! M' W, ^3 d; P
cout<<ValueOf(&t)<<endl;
( f+ a: S- s- \4 V0 ~- ] cout<<(long)&t<<endl;
$ [0 `6 J7 }) ?. |7 Z5 h 5 V9 w5 B# Y7 `: P
cout<<ValueOf(n)<<endl;
) g' n- A% Y' ] 5 Y v2 {$ q$ ?6 Y+ z
return 0; 1 P/ c1 r. g s3 a, L) l$ m9 ]
} 4 ]5 |) b, z% p8 W7 ~) X8 g
: O+ l) J7 H. ^4 |6 s
//运行结果
, V. B6 }% H8 a% [% z' L" \, y, L
! h1 q" v' Z% [$ ]6 P16 % Y( M+ T! }, @* k; |" F* [
0 6 w) P* i8 X+ C
4 : a) {3 L- {7 Z# z1 d2 N! v" ~
6 m! a/ n% p& d t; F4198425 8 j/ S. H, V0 B) w. M
6684088 . U$ s0 J1 [0 X- u* s
6684088 4 j! e5 K9 o. Y$ M. `. s
1000
* S! t4 @# h+ p: U* Y9 ~Press any key to continue |