c++ - Why can't I call protected virtual base class function in the derived class overridden function? -
class base { protected: virtual void show() { // stuff. } }; class derived : public base { protected: virtual void show() { // stuff. } }; class derived_2 : public derived { protected: virtual void show() { this->show(); // error: base::show() in accessible show(); // error: base::show() in accessible derived::show(); // error: base::show() in accessible } };
in above case calling virtual base class function (overridden in derived classes) gives error.
the error can find, call show
itsel leading infinite recursion , ending in stack overflow error.
but code compiles , run without warning :
class derived_2 : public derived { public: virtual void show() { //this->show(); // infinite recursion => stack overflow base::show(); // ok calls show() base derived::show(); // ok calls show() derived std::cout << "derived2" << std::endl; } };
(i declared public call directly in test)
Comments
Post a Comment