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_abFoo 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;}