小编Can*_*oti的帖子

调用成员函数指针

我在调用结构内的函数指针时遇到问题。我之前在类之外使用过这种方法,但现在我正在使用指向其他类方法的函数指针在类方法中尝试它......我收到一个编译器错误。这是我的课:

class Myclass
{
    int i;

    void cmd1(int)
    {}

    void cmd2(int)
    {}

    void trans()
    {
        const struct
        {
            std::string cmd;
            void (Myclass::*func)(int)
        }
        CmdTable[] =
        {
            { "command1", &Myclass::cmd1 },
            { "command2", &Myclass::cmd2 }
        };

        CmdTable[0].func(i);
        CmdTable[1].func(i);
    }
};
Run Code Online (Sandbox Code Playgroud)

线CmdTable[0].func(i);CmdTable[1].func(i);两个提供下列错误:错误:表达式必须具有(指针TO-)函数类型。

我意识到可能有更好的方法来做到这一点,但我很好奇为什么我写的东西不起作用。任何解释将不胜感激。

c++ pointers member-function-pointers

4
推荐指数
1
解决办法
92
查看次数

标签 统计

c++ ×1

member-function-pointers ×1

pointers ×1