C++ C++;使用它调用成员函数的语法';s函数指针
下面的示例使用指向Blah类的成员函数的函数指针。函数指针的语法对我来说很清楚。然而,在调用时,我不得不在C++ C++;使用它调用成员函数的语法';s函数指针,c++,function-pointers,C++,Function Pointers,下面的示例使用指向Blah类的成员函数的函数指针。函数指针的语法对我来说很清楚。然而,在调用时,我不得不在this->*funcPtr周围放上括号,我不知道为什么需要这样做。我想它与C++如何评估表达式的方式有关。使用的编译器是VS2008 #include <iostream> using namespace std; struct Blah { void myMethod(int i, int k) { cout << "Hi from
this->*funcPtr
周围放上括号,我不知道为什么需要这样做。我想它与C++如何评估表达式的方式有关。使用的编译器是VS2008
#include <iostream>
using namespace std;
struct Blah {
void myMethod(int i, int k) {
cout << "Hi from myMethod. Arguments: " << i << " " << k << endl;
}
typedef void (Blah::*blahFuncPtr)(int, int);
void travelSomething(blahFuncPtr funcPtr) {
(this->*funcPtr)(1, 2);
// w/o the brackets I get C2064 in VS 2008
// this->*funcPtr(1, 2);
}
};
int main() {
Blah blah;
blah.travelSomething(&Blah::myMethod);
cin.get();
return 0;
}
#包括
使用名称空间std;
结构废话{
void myMethod(inti,intk){
cout函数调用操作符()
比“指向成员的指针”操作符->*
具有更高的优先级
请看,例如,.好的,也许宏是个好主意。但这仍然不能回答我的问题。我想知道为什么..嗯,以及如何为函数指针定义宏,例如使用可变宏的参数?