rah*_*man 8 c++ function-pointers
我试图以更好的方式掌握指针功能概念.所以我有一个非常简单和有效的例子:
#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;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切都很好,现在我需要将类中的函数分组,并根据我使用的函数指针选择加或减.所以我只做一个小修改:
#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;
}
Run Code Online (Sandbox Code Playgroud)
而明显的错误是:
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)’
Run Code Online (Sandbox Code Playgroud)
因为我没有指定要调用哪个对象(我现在不想使用静态方法)
编辑: 几个评论和答案表明非静态版本(正如我所写)是不可能的.(感谢所有)所以,以下面的方式修改类也不会工作:
#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;
}
Run Code Online (Sandbox Code Playgroud)
生成此错误:
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)’
Run Code Online (Sandbox Code Playgroud)
我可以知道如何解决这个问题吗?
谢谢
声明成员方法的函数指针的语法是:
int (A::*plus)(int, int) = &A::add;
int (A::*minus)(int, int) = &A::subtract;
Run Code Online (Sandbox Code Playgroud)
要调用成员方法,请使用.*或 - >*运算符:
(a_plus.*plus)(7, 5);
Run Code Online (Sandbox Code Playgroud)
另请查看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;
}
Run Code Online (Sandbox Code Playgroud)