c++ - 函数指针演示

标签 c++ pointers function-pointers

检查下面的代码

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/

相关文章:

c - realloc 似乎没有重新分配内存

c++ - 如何减少定义指针函数的函数数量

c++ - c++ 中的 setprecision 是否是圆的?如果是这样,为什么我会看到这个?

c - 在循环链表的末尾插入在 C 中不起作用

C - 初始化指针数组

java - Java 8 方法引用与 'real' 函数指针有何不同?

我可以比较函数指针和函数是否相等吗?

c++ - 将程序从 x86 转换为 x64

c++ - 在 C++ 中为迭代器编写运算符 == 函数

c++ - gcc : inline of virtual function and especially destructor in my case 中内联的行为