Getting an error on calling a protected function for a non-this object of a base class. The compiler is GCC - but the same code worked under other flavors of GCC. What's going on, please - since when calling protected methods of a base class is verboten unless it's via this?

EDIT: sorry, my bad. This is all by the spec; in the other place where it worked, there was a friendship that I failed to notice. Please vote to close.

2 Answers
2

You don't have to access f through this - you can access it from any object of type B. For example, this will work inside B::g:

B b;
b.f();

The C++03 standard says (11.5):

When a friend or a member function of a derived class references a protected nonstatic member function or protected nonstatic data member of a base class, an access check applies in addition to those described earlier in clause 11. Except when forming a pointer to member (5.3.1), the access must be through a pointer to, reference to, or object of the derived class itself (or any class derived from that class)

So you can access f from an object of type B or derived from B - including but not limited to *this.