Skip to content

Instantly share code, notes, and snippets.

@hanxi
Created August 31, 2013 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanxi/6399464 to your computer and use it in GitHub Desktop.
Save hanxi/6399464 to your computer and use it in GitHub Desktop.
类成员函数指针指向类成员
#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