指向类成员的指针并非指针
from《C++ Common Knowledge》
#include <iostream> using namespace std; extern void fi(int); extern void fl(long); extern void fc(char); class Foo { public: Foo(){}; virtual ~Foo(){}; static void do_foo(int i){ cout<<"您好,您输入了i="<<i<<endl; } void to_str(){ cout<<"a_:"<<a_<<",b_:"<<b_<<endl; } int get_ab(){ return a_ + b_ ; } int b_; int a_; }; int main() { typedef void (*PF)(int); PF pf1 = Foo::do_foo; pf1(123); cout<<hex<<pf1<<dec<<endl; int Foo:: * pimC; //指向类Foo的int成员 typedef int (Foo:: * PFAB)(void); PFAB pfab = &Foo::get_ab; //指向类Foo的函数成员 get_ab Foo foo; Foo *pfoo = &foo; foo.a_ = 1; foo.b_ = 2; foo.to_str(); pimC = &Foo::a_; //指向类int成员的赋值 cout<< pimC <<",int成员指针的内容:" << foo.*pimC <<",PFoo->int:" << pfoo->*pimC <<endl; foo.*pimC = 110; foo.to_str(); cout<<pfab<<",get sum ab:"<<(foo.*pfab)() <<",指针:"<< ((pfoo->*pfab)()) <<endl; pimC = &Foo::b_; //指向类int成员的赋值 cout<< pimC << ",int成员指针的内容:" << foo.*pimC <<",PFoo->int:" << pfoo->*pimC <<endl; foo.*pimC = 220; foo.to_str(); cout<<pfab<<",get sum ab:"<<(foo.*pfab)() <<",指针:"<< ((pfoo->*pfab)()) <<endl; /* typedef union{ char ch; int i; } U; U u; u.i = 1; cout<<(int)u.ch<<endl; */ /* int i = 10; void * pv1 = &i; cout<< *(int *)pv1<<endl; */ // PF pf2 = fl; // PF pf3 = fc; // pf1(1); return 0; }