为什么我可以通过指向派生对象的基类指针访问派生私有成员函数? [英] Why can I access a derived private member function via a base class pointer to a derived object?

查看:30
本文介绍了为什么我可以通过指向派生对象的基类指针访问派生私有成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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.

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆