Derived class Private method is getting called

后端 未结 2 1515
失恋的感觉
失恋的感觉 2021-01-07 00:04

I have a Base class pointer pointing to derived class object. The method foo() is public in base class but private in derived class. Base class foo() is virtual. So when i c

2条回答
  •  太阳男子
    2021-01-07 00:35

    It's private method, but since it's virtual - it can be called.

    n3690 11.5/1

    The access rules (Clause 11) for a virtual function are determined by its declaration and are not affected by the rules for a function that later overrides it.

    Why this? Since

    n3690 11.5/2

    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.

提交回复
热议问题