As it's shown in the following code, I can call a non-static member function A::f
without instantiating an object of the class. This is only possible when the function is not bound to any other member. For example, I cannot call A::g
in a similar fashion.
It seems to me that the call to A::f
as shown in the code below behaves like calling a static member function. Is such a conclusion correct? How can this behavior be justified?
#include <iostream>
using namespace std;
struct A {
void f() { cout << "Hello World!"; }
void g() { cout << i; }
int i = 10;
};
int main() {
auto fPtr = reinterpret_cast<void(*)()>(&A::f);
(*fPtr)(); // OK
// auto gPtr = reinterpret_cast<void(*)()>(&A::g);
// (*gPtr)(); // Error!
return 0;
}