Created
August 31, 2013 17:02
-
-
Save hanxi/6399464 to your computer and use it in GitHub Desktop.
类成员函数指针指向类成员
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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