http://www.51cto.com/art/200712/62673.htm
里面给出的例子个人感觉不够好,应该用成员函数指针而不应该用函数指针。否则无法传递成员。。我给出的例子如下
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class Base
- {
- private:
- int i;
- public:
- Base():i(1)
- {}
- virtual void f()
- {
- cout << this->i << endl;
- }
- };
- class De: public Base
- {
- private:
- int j;
- public:
- De():
- j(10)
- {
- }
- void f()
- {
- cout << this->j << endl;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- //char abc[200];
- typedef void (Base::*FUNP)(void);
- Base* pb = new De();
- int* pvft = (int*)pb;//pvft is the point to virtual fun table;
- FUNP* pf = (FUNP*)(*pvft); //pf is the content of the firt element in vft (it should be the function pointer)
- FUNP ptoFunction = (FUNP)(*pf); //the pf should be a point to the function. why add another *????
- //FUNP ptoFunction = &Base::f;
- (pb->*ptoFunction)();
- int i;
- cin >> i;
- return 0;
- }