如何獲取test.calculate中的函數指針賦值(也許還有其他)?將C++函數指針分配給同一對象的成員函數
#include <iostream>
class test {
int a;
int b;
int add(){
return a + b;
}
int multiply(){
return a*b;
}
public:
int calculate (char operatr, int operand1, int operand2){
int (*opPtr)() = NULL;
a = operand1;
b = operand2;
if (operatr == '+')
opPtr = this.*add;
if (operatr == '*')
opPtr = this.*multiply;
return opPtr();
}
};
int main(){
test t;
std::cout << t.calculate ('+', 2, 3);
}
謝謝,你剛剛救了我的一天。 – toochin 2011-02-01 15:43:04