8

我试图以更好的方式掌握指针函数的概念。所以我有一个非常简单且有效的示例:

#include <iostream>

using namespace std;

int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}

int main()
{
    int  a, b;
    int  (*plus)(int, int);
    int  (*minus)(int, int);
    plus = &add;
    minus = &subtract;
    a = operation(7, 5, add);
    b = operation(20, a, minus);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}

到目前为止一切顺利,现在我需要将函数分组到一个类中,并根据我使用的函数指针选择加法或减法。所以我只是做了一个小的修改:

#include <iostream>

using namespace std;

class A
{
public:
int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
};

int main()
{
    int  a, b;
    A a_plus, a_minus;
    int (*plus)(int, int) = A::add;
    int (*minus)(int, int) = A::subtract;
    a = a_plus.operation(7, 5, plus);
    b = a_minus.operation(20, a, minus);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}

明显的错误是:

ptrFunc.cpp: In function ‘int main()’:
ptrFunc.cpp:87:29: error: invalid use of non-static member function ‘int A::add(int, int)’
ptrFunc.cpp:88:30: error: invalid use of non-static member function ‘int A::subtract(int, int)’

因为我没有指定要调用哪个对象(我现在不想使用静态方法)

编辑: 一些评论和答案表明非静态版本(如我所写)是不可能的。(感谢所有人)因此,以下列方式修改类也行不通:

#include <iostream>

using namespace std;

class A
{
    int res;
public:
    A(int choice)
    {
        int (*plus)(int, int) = A::add;
        int (*minus)(int, int) = A::subtract;
        if(choice == 1)
            res = operation(7, 5, plus);
        if(choice == 2)
            res = operation(20, 2, minus);
        cout << "result of operation = " << res;
    }
int add(int first, int second)
{
    return first + second;
}

int subtract(int first, int second)
{
    return first - second;
}

int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
};

int main()
{
    int  a, b;
    A a_plus(1);
    A a_minus(2);
    return 0;
}

产生了这个错误:

ptrFunc.cpp: In constructor ‘A::A(int)’:
ptrFunc.cpp:11:30: error: cannot convert ‘A::add’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’
ptrFunc.cpp:12:31: error: cannot convert ‘A::subtract’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’

请问我可以知道如何解决这个问题吗?

谢谢

4

4 回答 4

6

声明指向成员方法的函数指针的语法是:

int (A::*plus)(int, int) = &A::add;
int (A::*minus)(int, int) = &A::subtract;

要调用成员方法,请使用 .* 或 ->* 运算符:

 (a_plus.*plus)(7, 5);

也看看http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.80).aspx

希望这可以帮助。

完整代码:

     #include <iostream>

    using namespace std;

    class A
    {
    public:
    int add(int first, int second)
    {
        return first + second;
    }

    int subtract(int first, int second)
    {
        return first - second;
    }

    int operation(int first, int second, int (A::*functocall)(int, int))
    {
        return (this->*functocall)(first, second);
    }
    };

    int main()
    {
        int  a, b;
        A a_plus, a_minus;
        int (A::*plus)(int, int) = &A::add;
        int (A::*minus)(int, int) = &A::subtract;
        a = a_plus.operation(7, 5, plus);
        b = a_minus.operation(20, a, minus);
        cout << "a = " << a << " and b = " << b << endl;
        return 0;
    }
于 2013-04-02T05:05:03.343 回答
2

您不能那么容易地将非静态成员函数作为参数传递。为了您的需要,我相信最好覆盖运算符:http: //www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/

但是,如果您真的需要它们作为实际的成员函数 - 只需将它们设为静态即可。

于 2013-04-02T04:53:07.890 回答
2

您对代码所做的编辑仍然是错误的,因为它不会使成员函数成为静态的。您需要通过添加说明符使加法、减法等函数成为静态static

#include <iostream>

using namespace std;

class A
{
    int res;
public:
    A(int choice)
    {
        int (*plus)(int, int) = A::add;
        int (*minus)(int, int) = A::subtract;
        if(choice == 1)
            res = operation(7, 5, plus);
        if(choice == 2)
            res = operation(20, 2, minus);
        cout << "result of operation = " << res;
    }
static int add(int first, int second)
{
    return first + second;
}

static int subtract(int first, int second)
{
    return first - second;
}

static int operation(int first, int second, int (*functocall)(int, int))
{
    return (*functocall)(first, second);
}
};
于 2013-04-02T05:20:31.967 回答
2

请参阅下面的代码。函数调用在不使它们成为静态的情况下工作。

class A
{
  public:
  int add(int first, int second)
  {
      return first + second;
  }

  int subtract(int first, int second)
  {
      return first - second;
  }

  int operation(int first, int second, int(A::*functocall)(int, int))
  {
      return (this->*functocall)(first, second);
  }
};
//typedef int(A::*PFN)(int, int) ;
int main()
{
    int  a, b;
    A a_plus, a_minus;
    a = a_plus.operation(7, 5, &A::add);
    b = a_minus.operation(20, a, &A::subtract);
    cout << "a = " << a << " and b = " << b << endl;
    return 0;
}
于 2013-04-02T05:49:52.917 回答