有很多关于指向成员函数的指针的讨论,但我很难理解成员函数的隐藏 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))();
关于c++ - 指向成员函数的成员函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192160/