Changing Function Access Mode in Derived Class

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

    This is legal C++, §11.6/1 says:

    Access is checked at the call point using the type of the expression used to denote the object for which the member function is called (B* in the example above). The access of the member function in the class in which it was defined (D in the example above) is in general not known.

    As you noted, Child::Foo() is thus still accessible via the base class, which is in most cases undesired:

     Child* c = new Child;
     Base* b = c;
     c->Foo(); // doesn't work, Child::Foo() is private
     b->Foo(); // works, calls Child::Foo()
    

    Basically, the declaration you refer to in the expression dictates the access mode - but virtual functions undermine that as another function then the named one may actually be invoked.

提交回复
热议问题