分配 C++指向同一对象的成员函数的函数指针

发布于 2024-10-15 10:21:17 字数 615 浏览 3 评论 0原文

如何让 test.calculate 中的函数指针分配(也许还有其余部分)发挥作用?

#include <iostream>

class test {

    int a;
    int b;

    int add (){
        return a + b;
    }

    int multiply (){
        return a*b;
    }

    public:
    int calculate (char operatr, int operand1, int operand2){
        int (*opPtr)() = NULL;

        a = operand1;
        b = operand2;

        if (operatr == '+')
            opPtr = this.*add;
        if (operatr == '*')
            opPtr = this.*multiply;

        return opPtr();
    }
};

int main(){
    test t;
    std::cout << t.calculate ('+', 2, 3);
}

How do I get the function pointer assignments (and maybe the rest) in test.calculate to work?

#include <iostream>

class test {

    int a;
    int b;

    int add (){
        return a + b;
    }

    int multiply (){
        return a*b;
    }

    public:
    int calculate (char operatr, int operand1, int operand2){
        int (*opPtr)() = NULL;

        a = operand1;
        b = operand2;

        if (operatr == '+')
            opPtr = this.*add;
        if (operatr == '*')
            opPtr = this.*multiply;

        return opPtr();
    }
};

int main(){
    test t;
    std::cout << t.calculate ('+', 2, 3);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅紫色的梦幻 2024-10-22 10:21:17

您的代码存在几个问题。

首先,int (*opPtr)() = NULL; 不是指向成员函数的指针,而是指向自由函数的指针。像这样声明一个成员函数指针:

int (test::*opPtr)() = NULL;

其次,在获取成员地址时需要指定类作用域函数,如下所示:

if (operatr == '+') opPtr = &test::add;
if (operatr == '*') opPtr = &test::multiply;

最后,要通过成员函数指针进行调用,有特殊的语法:

return (this->*opPtr)();

这是一个完整的工作示例:

#include <iostream>

class test {

    int a;
    int b;

    int add (){
        return a + b;
    }

    int multiply (){
        return a*b;
    }

    public:
    int calculate (char operatr, int operand1, int operand2){
        int (test::*opPtr)() = NULL;

        a = operand1;
        b = operand2;

        if (operatr == '+') opPtr = &test::add;
        if (operatr == '*') opPtr = &test::multiply;

        return (this->*opPtr)();
    }
};

int main(){
    test t;
    std::cout << t.calculate ('+', 2, 3);
}

There are several problems with your code.

First, int (*opPtr)() = NULL; isn't a pointer to a member function, its a pointer to a free function. Declare a member function pointer like this:

int (test::*opPtr)() = NULL;

Second, you need to specify class scope when taking the address of a member function, like this:

if (operatr == '+') opPtr = &test::add;
if (operatr == '*') opPtr = &test::multiply;

Finally, to call through a member function pointer, there is special syntax:

return (this->*opPtr)();

Here is a complete working example:

#include <iostream>

class test {

    int a;
    int b;

    int add (){
        return a + b;
    }

    int multiply (){
        return a*b;
    }

    public:
    int calculate (char operatr, int operand1, int operand2){
        int (test::*opPtr)() = NULL;

        a = operand1;
        b = operand2;

        if (operatr == '+') opPtr = &test::add;
        if (operatr == '*') opPtr = &test::multiply;

        return (this->*opPtr)();
    }
};

int main(){
    test t;
    std::cout << t.calculate ('+', 2, 3);
}
如梦亦如幻 2024-10-22 10:21:17

就像这个int (test::*opPtr)() = NULL;。请参阅http://www.parashift.com/ c++-faq-lite/pointers-to-members.html#faq-33.1

编辑:还可以使用 if (operatr == '+') opPtr = &test::add;而不是 [..] = this.addreturn (this->(opPtr ))(); 而不是 return opPtr();。事实上,使用常见问题解答中提到的 typedef 和宏,以及可能的成员函数参数,而不是类成员 ab

Like this int (test::*opPtr)() = NULL;. Refer http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.1

Edit: Also use if (operatr == '+') opPtr = &test::add; instead of [..] = this.add and return (this->(opPtr))(); instead of return opPtr();. In fact, use typedefs and macros like the FAQ says and probably member function paramaters instead of class members a and b.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文