为什么我可以通过指向派生对象的基类指针访问派生私有成员函数?

Why can I access a derived private member function via a base class pointer to a derived object?(为什么我可以通过指向派生对象的基类指针访问派生私有成员函数?)
本文介绍了为什么我可以通过指向派生对象的基类指针访问派生私有成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>

using namespace std;
class base
{
public:
    virtual void add() {
        cout << "hi";
    }
};

class derived : public base
{
private:
    void add() {
        cout << "bye";
    }
};

int main()
{
    base *ptr;
    ptr = new derived;
    ptr->add();
    return 0;
}

输出是再见

我对如何实现没有问题.我了解您使用 vtables,并且 derived 的 vtable 包含新的 add() 函数的地址.但是 add() 是私有的,当我尝试在类外访问它时,编译器不应该产生错误吗?不知怎的,这似乎不对.

I dont have a problem with how this is implemented. I understand you use vtables and the vtable of derived contains the address of the new add() function. But add() is private shouldn't compiler generate an error when I try to access it outside the class? Somehow it doesn't seem right.

推荐答案

add()derived中只是私有的,而静态类型 你拥有的是 base* - 因此 base 的访问限制适用.
通常,您甚至无法在编译时知道指向 base 的指针的动态类型是什么,例如根据用户输入进行更改.

add() is only private in derived, but the static type you have is base* - thus the access restrictions of base apply.
In general you can't even know at compile time what the dynamic type of a pointer to base will be, it could e.g. change based on user input.

这是根据 C++03 §11.6:

虚函数的访问规则(第 11 条)由其声明决定,不受稍后覆盖它的函数规则的影响.
[...]在调用点使用表达式类型检查访问,该表达式用于表示调用成员函数的对象[...].对定义它的类中的成员函数的访问 [...] 通常是未知的.

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.
[...] 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 [...]. The access of the member function in the class in which it was defined [...] is in general not known.

这篇关于为什么我可以通过指向派生对象的基类指针访问派生私有成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Polymorphic copy-constructor with type conversion(具有类型转换的多态复制构造函数)
Cast to a Child(铸造给孩子)
How to use virtual functions to achieve a polymorphic behavior in C++?(如何使用虚函数在 C++ 中实现多态行为?)
Polymorphism is not working with function return values of same data type (Base and Inherited class)(多态性不适用于相同数据类型(基类和继承类)的函数返回值)
Size of class with virtual function adds extra 4 bytes(具有虚函数的类的大小增加了额外的 4 个字节)
Right design pattern to deal with polymorphic collections of objects(正确的设计模式来处理对象的多态集合)