我试图以更好的方式掌握指针功能概念。所以我有一个非常简单和有效的例子:
#include <iostream>
using namespace std;
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
int main()
{
int a, b;
int (*plus)(int, int);
int (*minus)(int, int);
plus = &add;
minus = &subtract;
a = operation(7, 5, add);
b = operation(20, a, minus);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}
到目前为止一切顺利, 现在我需要将类中的函数分组,并根据我使用的函数指针选择加或减。所以我只做了一个小修改:
#include <iostream>
using namespace std;
class A
{
public:
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
};
int main()
{
int a, b;
A a_plus, a_minus;
int (*plus)(int, int) = A::add;
int (*minus)(int, int) = A::subtract;
a = a_plus.operation(7, 5, plus);
b = a_minus.operation(20, a, minus);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}
,明显的错误是:
ptrFunc.cpp: In function ‘int main()’:
ptrFunc.cpp:87:29: error: invalid use of non-static member function ‘int A::add(int, int)’
ptrFunc.cpp:88:30: error: invalid use of non-static member function ‘int A::subtract(int, int)’
因为我没有指定要调用哪个对象(我现在不想使用静态方法)
修改 几条评论和答案表明非静态版本(正如我所写)是不可能的。(感谢所有人) 所以, 以下列方式修改类也不会起作用:
#include <iostream>
using namespace std;
class A
{
int res;
public:
A(int choice)
{
int (*plus)(int, int) = A::add;
int (*minus)(int, int) = A::subtract;
if(choice == 1)
res = operation(7, 5, plus);
if(choice == 2)
res = operation(20, 2, minus);
cout << "result of operation = " << res;
}
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
};
int main()
{
int a, b;
A a_plus(1);
A a_minus(2);
return 0;
}
生成了这个错误:
ptrFunc.cpp: In constructor ‘A::A(int)’:
ptrFunc.cpp:11:30: error: cannot convert ‘A::add’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’
ptrFunc.cpp:12:31: error: cannot convert ‘A::subtract’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’
我可以知道如何解决这个问题吗?
感谢
答案 0 :(得分:5)
声明成员方法的函数指针的语法是:
int (A::*plus)(int, int) = &A::add;
int (A::*minus)(int, int) = &A::subtract;
要调用成员方法,请使用。*或 - &gt; * operator:
(a_plus.*plus)(7, 5);
另请查看http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.80).aspx
希望这有帮助。
完整代码:
#include <iostream>
using namespace std;
class A
{
public:
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (A::*functocall)(int, int))
{
return (this->*functocall)(first, second);
}
};
int main()
{
int a, b;
A a_plus, a_minus;
int (A::*plus)(int, int) = &A::add;
int (A::*minus)(int, int) = &A::subtract;
a = a_plus.operation(7, 5, plus);
b = a_minus.operation(20, a, minus);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}
答案 1 :(得分:2)
你不能将非静态成员函数作为参数传递给easy。根据您的需求,我认为最好覆盖运营商:http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/
但是如果你真的需要它们作为实际的成员函数 - 只需将它们设置为静态。
答案 2 :(得分:2)
您对代码所做的编辑仍然是错误的,因为它不会使成员函数保持静态。您需要通过添加static
说明符来使加法,减法等函数保持静态:
#include <iostream>
using namespace std;
class A
{
int res;
public:
A(int choice)
{
int (*plus)(int, int) = A::add;
int (*minus)(int, int) = A::subtract;
if(choice == 1)
res = operation(7, 5, plus);
if(choice == 2)
res = operation(20, 2, minus);
cout << "result of operation = " << res;
}
static int add(int first, int second)
{
return first + second;
}
static int subtract(int first, int second)
{
return first - second;
}
static int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
};
答案 3 :(得分:2)
请参阅以下代码。函数调用正在工作而不会使它们静止。
class A
{
public:
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int(A::*functocall)(int, int))
{
return (this->*functocall)(first, second);
}
};
//typedef int(A::*PFN)(int, int) ;
int main()
{
int a, b;
A a_plus, a_minus;
a = a_plus.operation(7, 5, &A::add);
b = a_minus.operation(20, a, &A::subtract);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}