c++ - 指向成员函数的成员函数指针

标签 c++ class

有很多关于指向成员函数的指针的讨论,但我很难理解成员函数的隐藏 const 状态的问题。谁能给我一个更简单的答案,我在这里做错了什么?

class Entity
{
public:
    Entity();
    void(Entity::*update_function)();
private:
    void update_mode_1() {
    }
};

Entity::Entity()
{
    update_function = update_mode_1;
    //error C3867: 'Entity::update_mode1': non-standard syntax; use '&' to create a pointer to member
}

void test_init() {
    Entity obj;
    obj.update_function();
    //Error: Expression preceding parenthesis of apparent call must have (pointer-to-) function type
}

最佳答案

这里是如何解决错误的解决方案,无论如何,它似乎与“成员函数的隐藏常量状态”无关。

error C3867: 'Entity::update_mode1': non-standard syntax; use '&' to create a pointer to member

正如错误消息所述,使用 & 创建指向成员的指针。

update_function = &Entity::update_mode_1;

Error: Expression preceding parenthesis of apparent call must have (pointer-to-) function type

使用pointer-to-member access operator (例如运算符.*)来调用它。

(obj.*(obj.update_function))();

LIVE

关于c++ - 指向成员函数的成员函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192160/

相关文章:

c++ - 调用多参数模板函数

c++ - 如何查找 Outlook 邮件文件 (.msg) 的代码页 ID

python - 每次调用类中的方法时运行一个函数

C++ getline() 不结束输入

c++ - 如何在 Linux ubuntu 中设置 C++ 环境变量?

ios - 从 NSObject 类更改 UISwitch 的状态

java - 许多简单类实例,我的做法是最好的解决方案吗?

c++ - 需要快速帮助 基类未定义

c++ - 试图掌握动态层次类关系的装饰器设计

c++ - 使用不同的预处理器宏从同一源代码树构建多个目标