Changing Function Access Mode in Derived Class

前端 未结 4 673
囚心锁ツ
囚心锁ツ 2020-11-28 07:45

Consider the following snippet:

struct Base
{
  virtual ~Base() {}

  virtual void Foo() const = 0; // Public
};

class Child : public Base
{
  virtual void          


        
4条回答
  •  醉话见心
    2020-11-28 08:07

    It seems to compile and call the right method.

    Remember that access specifiers are there to help a disciplined programmer, not to prevent all attempts to circumvent it at all costs.

    In this particular case, Child has no business making the overridden virtual function private: isn't it supposed to implement the public interface of Base, so the "is-a" relationship holds? (If you didn't use public inheritance, which means "Child is a Base", your trick wouldn't work.)

提交回复
热议问题