简体   繁体   中英

Member function pointer point to method of another object

Is it possible to have a member function pointer of an object point to a method of another object? If so, please give an example.

Is it possible to have a member function pointer of an object point to a method of another object?

Yes.

If so, please give an example.

I'll give you two.

struct A {
 void f() {}
};
struct B {
 void (A::*p)();
};

int main () {
 A a;
 B b;
 b.p = &A::f;
 (a.*b.p)();

 A* pA = &a;
 B* pB = &b;
 (pA->*pB->p)();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM