函数指针获取成员函数地址(包括虚函数) 发表于 2014-09-03 | 分类于 语言 ++123456789101112131415161718192021222324252627282930313233343536373839class Base{public: void func() { printf("Enter Base::func()\n"); } virtual void virtual_func() { printf("Enter Base::virtual_func()\n"); }};class Derived : public Base{public: void func() { printf("Enter Derived::func()\n"); } virtual void virtual_func() { printf("Enter Derived::virtual_func()\n"); }};typedef void (Base::*p_base_func)();int main(int argc, char* argv[]) { Derived d; Base b; Base *pb = &d; p_base_func pbf = &Base::func; printf("-- Call via pointer to normal function of base\n"); (pb->*pbf)(); (d.*pbf)(); cout << endl; //cout << (pb->*pbf) << endl; cout << endl << endl; pbf = &Base::virtual_func; printf("-- Call via pointer to virtual function of base\n"); (pb->*pbf)(); (d.*pbf)(); //cout << (pb->*pbf) << endl; system("pause"); return 0; } 注释部分会发生编译器内部错误。我也不知道为什么? 看代码,为成员函数添加一个引用(支持虚函数)。然后利用对象调用函数。貌似,只能利用对象才能成功。