类成员函数作为函数参数出现error C3867:非标准语法;请使用“&”来创建指向成员的指针

有的时候一个泛函可能会采用函数或函数对象(functor)做参数,这时候我们可能想要使用类的成员函数来做参数,但是这时候会出现error C3867: 非标准语法;请使用 "&" 来创建指向成员的指针,比如下面例子:

 #include <iostream>
  using namespace std;
  class TestC 
  {
  private:
      double a, b;
  public:
      TestC(double a_,double b_):a(a_),b(b_){}
      double testFun(double x);
 };
 template<class Func>
 double testTemplateFun(double input, Func proccess) 
 {
     return proccess(input);
 }
 
 int main()
 {
     double a = 10;
     TestC t(2, 3);
     cout << "输出结果为:" << testTemplateFun(a, t.testFun);
     system("pause");
     return 0;
 }
 
 double TestC::testFun(double x)
 {
     return a*x + b;
 }

这时候我们按照提示将t.testFun前面加上&又会出现error C2276: “&”: 绑定成员函数表达式上的非法操作,那么这到底是什么错误?

其实这里是因为类的成员函数默认带有一个this指针参数,那么它作为泛函的参数其实就不匹配了,因为泛函中的Func类型并没有this指针,所以我们可以把TestC类中的testFun(double x)函数声明为静态(static)的,这样就不带有this指针;但是静态的函数又不能访问非静态的成员,像这里TestC的testFun函数访问了非静态的成员变量a,b,就会出错,那要怎么办?这里其实就是相当于泛函中要求单参函数,而使用的是双参函数,可以自己定义一个适配类,适配双参函数为单参,如下例:

 #include <iostream>
  using namespace std;
  class TestC 
  {
  private:
      double a, b;
  public:
      TestC(double a_,double b_):a(a_),b(b_){}
      double testFun(double x);
 };
 template<class Func>
 double testTemplateFun(double input, Func proccess) 
 {
     return proccess(input);
 }
 class TestCAdapter 
 {
 private:
     TestC *t;
 public:
     TestCAdapter(TestC *t_):t(t_){}
     double operator()(double x);
 };
 int main()
 {
     double a = 10;
     TestC t(2, 3);
     //cout << "输出结果为:" << testTemplateFun(a, t.testFun) << endl;
     TestCAdapter ad(&t);
     cout << "输出结果为:" << testTemplateFun(a, ad) << endl;
     system("pause");
     return 0;
 }
 
 double TestC::testFun(double x)
 {
     return a*x + b;
 }
 
 double TestCAdapter::operator()(double x)
 {
     return t->testFun(x);
 }

--------------------- 
作者:liyunxin_c_language 
来源:CSDN 
原文:https://blog.csdn.net/liyunxin_c_language/article/details/83188176 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/majalis_C/article/details/89519674