There is a lot of talk of pointers to member functions but I'm having trouble grasping the issue with the hidden const state of member functions. Can anyone give me a simpler answer what I'm doing wrong here?
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
}
答案 0 :(得分:4)
Here's the solutions how to solve the errors, anyway, it seems has nothing to do with "hidden const state of member functions".
error C3867: 'Entity::update_mode1': non-standard syntax; use '&' to create a pointer to member
As the error message said, 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
Use pointer-to-member access operator (such as the operator .*
) to call it.
(obj.*(obj.update_function))();