Why can B::Func call A::Func using syntax that makes it look like a static method call? Shouldn’t this fail because it is an instance method?
class A {
public:
void Func() {
printf( "test" );
}
};
class B : private A {
public:
void Func() {
A::Func(); // why does it work? (look below in main())
}
};
int main() {
B obj;
obj.Func();
// but we cannot write here, because it's not static
// A::Func();
return 0;
}
That isn’t “called like static”. That’s merely the syntax used to explicitly specify which member function to call. Even if
Funcwere virtual, that syntax could be used to call a base class version.Edit:
obj.A::Func()would be invalid actually, in your case, because the inheritance is private, somaincannot see thatAis a base ofB. I’ve changedB‘s inheritance to public to make the answer correct, but it would otherwise still work outside of the class, when in afriendfunction.