cannot call base class protected functions?

Protected member functions can only be called inside the base class or in its derived class. You cannot call them outside your class. Outside calling means calling a member function of a class-typed variable.

So

virtual f1(B*b) { return f2(b); }

is ok, because f2 operates on the class itself. (called inside)

But

virtual f2(B*b) { return b->f2(this); }

won’t compile, because f2 operates on b not the class D itself. (called outside) It’s illegal.

To fix it B::f2 should be public.

Leave a Comment