指向成員函數(shù)的函數(shù)指針我希望將函數(shù)指針設(shè)置為一個類的成員,它是指向同一個類中另一個函數(shù)的指針。我這么做的原因很復(fù)雜。在本例中,我希望輸出為“1”。class A {public:
int f();
int (*x)();}int A::f() {
return 1;}int main() {
A a;
a.x = a.f;
printf("%d\n",a.x())}但這在編譯上失敗了。為什么?
3 回答

三國紛爭
TA貢獻1804條經(jīng)驗 獲得超7個贊
class A {public: int f(); int (A::*x)(); // <- declare by saying what class it is a pointer to};int A::f() { return 1;}int main() { A a; a.x = &A::f; // use the :: syntax printf("%d\n",(a.*(a.x))()); // use together with an object of its class}
a.x
a
a
.*

慕萊塢森
TA貢獻1810條經(jīng)驗 獲得超4個贊
int (*x)()
int (A::*x)(void) = &A::f;
.
- 3 回答
- 0 關(guān)注
- 629 瀏覽
添加回答
舉報
0/150
提交
取消