There is an example about the pointer to a member function.
#include <stdlib.h>
#include <iostream>
using namespace std;
class B {
public:
virtual void set(int val) {
val_ = val;
cout<<"B : "<<val_<<endl;
}
protected:
int val_;
};
class D : public B {
public:
virtual void set(int val) {
val_ = val;
cout<<"D : "<<val_<<endl;
}
};
int main() {
B b;
D d;
B *pb = &d;
void (B::*fn) (int) = &B::set;
(pb->*fn)(12);
//void (D::*fx)(int) = &B::bset;
//(d.*fx)(11);
return 0;
};
June 18th Tuesday (六月 十八日 木曜日)
最新推荐文章于 2025-08-21 09:06:15 发布
本文通过一个具体的示例介绍了如何使用成员函数指针来调用基类和派生类中的方法。示例中定义了两个类:基类B和派生类D,并展示了如何通过指向成员函数的指针来调用这些类的方法。
1325

被折叠的 条评论
为什么被折叠?



