Changing Function Access Mode in Derived Class

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

    It is perfectly legal C++. You are simply defining a new method in Child class.

    Now does it do what you want it to do, that's an other question. I believe the access mode is not part of the method signature, which means that calling Base's Foo virtual method does eventually call Child's Foo method.

    So here's the conclusion : it is legal c++ and it works the way you'd expect.

    I am not taking into consideration the line child.Foo(); which can't work because there is no doubt it is trying to access Child's private Foo() method.

提交回复
热议问题