Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
类成员函数指针指向类成员
#include <iostream>
using namespace std;
class A {
public:
A() {
q=&A::funcA;
cout << "q=" << q << endl;
//(this ->* ((A*)this)->A::q) ();
(this ->* q) ();
q=&A::funcB;
cout << "q=" << q << endl;
(this ->* q) ();
}
void funcA() {
cout << "funcA" << endl;
}
void funcB() {
cout << "funcB" << endl;
}
void (A::*q)();
void (*pq) ();
};
void func() {
cout << "func" << endl;
}
int main(int argc, char** argv) {
A a;
a.q = &A::funcA;
(a.*(a.q)) ();
a.q = &A::funcB;
(a.*(a.q)) ();
a.pq = func;
a.pq();
void (A::*p)() = &A::funcA;
cout << "p=" << p << endl;
(a.*p)();
p = &A::funcB;
cout << "p=" << p << endl;
(a.*p)();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment