Changing Function Access Mode in Derived Class

前端 未结 4 676
囚心锁ツ
囚心锁ツ 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:22

    Yes, changing the access mode in derived classes is legal.

    This is similar in form but different in intent to the Non-Virtual Interface idiom. Some rationale is given here:

    The point is that virtual functions exist to allow customization; unless they also need to be invoked directly from within derived classes' code, there's no need to ever make them anything but private.

    As to why you would actually make something public in base but private in derived without private or protected inheritance is beyond me.

提交回复
热议问题