Pass Member Function Pointers to parent class
我正在研究C++中的PACMN游戏,但是已经遇到了一个成员函数指针的问题。我有两个阶级,分别是
我尝试传递
我尝试传递
以下是相关的内容:(我删掉了大部分内容,这样你只会看到这个问题需要什么)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class cMouvement { protected: int curDirection = -3; // Variables that are used in the 'movingUp, etc' functions. int newDirection = -3; // And therefore can't be static public: void checkIntersection(void (*function)(int), bool shouldDebug){ // Whole bunch of 'If's that call the passed function with different arguments } |
然后是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class pacman : public cMouvement { void movingUp(int type){ // Blah blah blah } // movingDown, movingLeft, movingRight... (Removed for the reader's sake) public: /*Constructor function*/ void move(bool shouldDebug){ if (curDirection == 0) {checkIntersection(&movingUp, false);} else if (curDirection == 1) {checkIntersection(&movingRight, false);} else if (curDirection == 2) {checkIntersection(&movingDown, false);} else if (curDirection == 3) {checkIntersection(&movingLeft, false);} } }; |
您需要的是提供成员函数的签名,而不是常规函数的签名。
1 | void checkIntersection(void (ghost::*)(int), bool shouldDebug){ |
在C++中将成员函数传递为参数
如果您真的需要从
为什么不在