English 中文(简体)
聚变和数据隐藏: 基础阶层是否凌驾于或无视衍生的阶层准入限制?
原标题:Polymorphism and data hiding: Does a base class override or ignore a derived class access restrictions?

请参看以下法典:

#include <iostream>

using namespace std;

class Base {
public:
    virtual void Message() = 0;
};

class Intermediate : public Base {

};

class Final : public Intermediate {
    void Message() {
        cout << "Hello World!" << endl;
    }
};

int main() {
    Final final;
    /* Wont work (obviously):
    Final* finalPtr = &final;
    finalPtr->Message();
    */
    // Works:
    Intermediate* finalPtr = &final; // or Base* finalPtr = &final;
    finalPtr->Message();

    return 0;
}

Take note of the following:

  1. In the abstract Base class, the pure virtual function message() is public
  2. Intermediate class (also abstract) inherits from Base class (Does message() function remain public pure virtual in Intermediate?)
  3. Final class inherits from Intermediate class and message() function is private (by default)
  4. In main, an object of type Final is created
  5. An Intermediate pointer to the Final object is created

Question: If you run the program, the line finalPtr->Message(); successfully invokes Final s implementation of message() function though its private. How does that happen? Does a base class override or ignore a derived class access restrictions?

Related Question: In relation to (2.) above, what is the right way to define Intermediate class? Does one need to re-declare the pure virtual function message() from the base class bearing in mind that Intermediate class is not intended to provide an implementation.

<>光> 对《守则》进行了数字火星孔径仪和微软视像素演化器(cl)的测试,并在两种情况下都做了公正的罚款。

最佳回答

如何做到这一点? 基础阶层是否凌驾于或无视衍生的阶层准入限制?

With Public Inheritance all public members of the Base class become public members of the derived class. So Yes Message() is public function in Intermediate.

该职能按基类(Intermediate)规定,职能在基类公开。 动态的派遣(即实际要求转授班职能)只是在操作时间才发生,因此这项工作是可行的。

以上原因是,在经营时间,进入投机者没有任何意义,准入投机规则只有在汇编时间才能解决和有效。

如果你把这一职能放在衍生班子上,那么汇编者在汇编时就会发现,<代码>Message(<>>在Final中被宣布为<> 私人,从而造成错误。


虽然从摘要类别中得出,衍生的类别MUSTALL提供了定义,但如未能做到这一点,Derived类也将成为摘要类别。

在这里,<代码>Intermediate类别为“摘要”类,只要你不需要制造这一类物体,该类物品将处以罚款。 请注意,您可设立摘要类的协调人。

问题回答

在C++中,虚拟用户和用户相互排斥。 因此,在C++中,对虚拟方法的使用可以缩小,而在C#或Java,则是不可能的。

当你试图通过基数班点获得虚拟功能时,汇编者汇编了该守则,因为基类虚拟功能是公开的。

In your commented code, the virtual function having restricted access is called via the Final class pointer. Hence the compilation error.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签