检查下面的代码
int add(int a, int b)
{
return a + b;
}
void functionptrdemo()
{
typedef int *(funcPtr) (int,int);
funcPtr ptr;
ptr = add; //IS THIS CORRECT?
int p = (*ptr)(2,3);
cout<<"Addition value is "<<p<<endl;
}
在我尝试将函数分配给具有相同函数签名的函数 ptr 的地方,它显示编译错误为 error C2659: '=' : function as left operand
最佳答案
很可能您打算写的不是:
typedef int *(funcPtr) (int,int);
但是:
typedef int (*funcPtr) (int,int);
关于c++ - 函数指针演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2590553/