当从公共接口继承时,为什么实现是公共的还是私有的并不重要?

发布于 2024-11-17 03:14:57 字数 662 浏览 2 评论 0原文

我可能只是累了或者距离 C++ 太远了,但今天这个真的让我感到惊讶:

#include <iostream>

class Interface
{
public:
    virtual int aa() const = 0;
    virtual int bb() const = 0;
};

class Usage : public Interface
{
private:
    virtual int aa() const
    {
        int a = 10 * 10;
        return a;
    }

    virtual int bb() const
    {
        int b = 20 * 20;
        return b;
    }
};

int main(int argc, char* argv[])
{
    Interface* i = new Usage();
    std::cout << i->bb() << std::endl;

    return 0;
}

我希望编译器和/或链接器会抱怨错误的函数签名或至少抱怨缺少实现。考虑到这是有效的,那么当 public/protected/private 修饰符被顶级类声明隐藏时,它的含义是什么?

这个规则在C++中是如何调用的?

I might be very well just tired or too long far from C++ but this one really surprised me today:

#include <iostream>

class Interface
{
public:
    virtual int aa() const = 0;
    virtual int bb() const = 0;
};

class Usage : public Interface
{
private:
    virtual int aa() const
    {
        int a = 10 * 10;
        return a;
    }

    virtual int bb() const
    {
        int b = 20 * 20;
        return b;
    }
};

int main(int argc, char* argv[])
{
    Interface* i = new Usage();
    std::cout << i->bb() << std::endl;

    return 0;
}

I'd expect compiler and/or linker would complain about either bad function signature or at least about missing implementation. Considering this is working and ok, what is the meaning of public/protected/private modifiers when it's hidden by the top class declaration?

How does this rule call in C++ ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少年亿悲伤 2024-11-24 03:14:57

标准第 11.6.1 段对此进行了规定:

访问规则(第 11 条)
虚函数由其决定
声明并不受
稍后函数的规则
覆盖它。 [示例 - 与您的基本相同]
在呼叫点检查访问权限
使用所用表达式的类型
来表示对象
成员函数被称为 .访问权限
类中的成员函数
它的定义通常不是
已知。

This is specified in paragraph 11.6.1 of the standard:

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. [Example - basically same as yours]
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.

赴月观长安 2024-11-24 03:14:57
Interface* i = new Usage();
std::cout << i->bb() << std::endl;

这是有效的,因为函数名称是根据对象的静态类型解析的。

这里的对象是 i ,其 static 类型是 Interface* ,它有一个 public 函数名称 bb( )。因此,编译器不会发现任何问题,因为调用成员函数的要求满足了。

另请注意,可访问性(publicprivateprotected)是编译时构造。在运行时,没有这样的事情。编译器只能在编译时检测到任何与可访问性相关的违反规则的情况。它无法知道运行时发生了什么。

因此,即使 i 指向一个类型为 Usage 的对象,该对象已在 private 部分中定义了 bb() ,编译器对此没有问题,如前所述,istatic 类型仍然是具有 publicInterface*代码> 函数bb()。编译器不会关心对象的动态类型以及它如何覆盖函数,因为它不能,正是因为它的动态;它是在运行时确定的。

Interface* i = new Usage();
std::cout << i->bb() << std::endl;

This is working because the function name is resolved based on the static type of the object.

Here the object is i whose static type is Interface* which has a public function name bb(). Hence, the compiler doesn't see any problem, as the requirement to call a member function meets it.

Also note that accessibilities (public, private and protected) are compile-time constructs. At runtime, there is no such thing. Compiler can detect any violation of rules related to accessibility at compile time only. It cannot know what happens at runtime.

So even if i points to an object whose type is Usage which has defined bb() in the private section, the compiler is fine with it, as noted before the static type of i is still Interface* which has a public function bb(). The compiler doens't bother with the dynamic type of object and how it overrides the function, because it cannot, precisely for the reason that its dynamic; its determined at runtime.

旧话新听 2024-11-24 03:14:57

private 意味着你不能继承它。您可以继承 protectedpublic,但没有什么可以阻止您将可见性限制在较低级别(即 publicprotected< /code> 或 private; 或 protectedprivate) 在你的顶级类中。

private means you can't inherit from it. protected or public you can inherit from but there is nothing stopping you limiting the visibility to a lower level (i.e. public to protected or private; or protected to private) in your top class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文