当函数参数使用引用传参时,怎样通过函数指针来调用它呢?

来源:百度知道 编辑:UC知道 时间:2024/06/04 07:36:09
例如:
int max(int &a,int &b)
{ }
那么怎样声明函数指针呢?
我用 int (*pfn)(int &,int &);结果出现错误
正确的形式是怎么样的,高手指教!!
不加& 编译还是通不过 错误提示:
D:\我的文档\vc6.0\classtest.cpp(70) : error C2440: '=' : cannot convert from 'int (__cdecl *)(int &,int &)' to 'int (__cdecl *)(int,int)'

没问题哦,我编译能通过
#include <iostream.h>
int f(int &a,int&b){return a+b;}
main()
{
int(*p)(int &,int&);int a=3,b=4;
p=f;
cout <<p(a,b)<<endl;
}
你出现编译问题应该是max没写主体语句吧,至少要return int();哦
而且引用的话,最好用const int&格式

这样是可以的啊,如果你要传入的是数字,比如(*pfn)(3,4)则要在前面加const int (*pfn)(const int &,const int &);
int max(const int &a,const int &b)

你到底是想用指针调用函数.还是调用参数?

不要+&