Reputation: 2012
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;
}
Upvotes: 0
Views: 108
Reputation:
That isn't "called like static". That's merely the syntax used to explicitly specify which member function to call. Even if Func
were virtual, that syntax could be used to call a base class version.
class B : public A {
public:
void Func() {
this->A::Func(); /* this-> can be omitted */
}
};
int main() {
B obj;
obj.A::Func();
return 0;
}
Edit: obj.A::Func()
would be invalid actually, in your case, because the inheritance is private, so main
cannot see that A
is a base of B
. I've changed B
's inheritance to public to make the answer correct, but it would otherwise still work outside of the class, when in a friend
function.
Upvotes: 5