What is the reason why member function pointers cannot point to member functions?
struct A { void g() {}; void f() const {} };
Later in code:
void (A::* fun)() = &A::f;
This code creates:
error: cannot convert 'void (A::*)()const' to 'void (A::*)()' in initialization
Of course, it compiles with &A::g instead of &A::f .
In the opposite situation:
void (A::* fun)() const = &A::g;
Mistake:
error: cannot convert 'void (A::*)()' to 'void (A::*)()const' in initialization
The second case is pretty clear. const pointer should not modify an object, so it cannot hold a function that does this. But why is it impossible to assign a const member function to a non-const member function as in the first case?
It looks usually for ordinary pointers, where dropping the const to non-const would make it possible to change the value, but I donβt see the point here where const-correctness is checked in the function definition, before that purpose.
c ++ pointers const function-pointers
pkubik
source share