class A
{
public:
int add(int a,int b)
{
int c = a+b;
return c;
}
};
typedef int (*pfun)(int,int);
int main()
{
pfun q =(pfun)&A::add;
int c = (*q)(7,8);
cout<<c<<endl;
return 1;
}
输出结果不是15,而是一个随机值,怎么回事?难道成员函数指针转为一般函数指针不安全吗?
不能这么转换,成员函数其实还有一个隐藏的this指针参数,在最后,而你普通函数,没有这个参数,导致堆栈不平衡。
把add定义为类的静态成员函数。static试试