多态和数据隐藏:基类是否重写或忽略派生类的访问限制? [英] Polymorphism and data hiding: Does a base class override or ignore a derived class' access restrictions?

查看:122
本文介绍了多态和数据隐藏:基类是否重写或忽略派生类的访问限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看以下代码列表:

  #include< iostream> 

using namespace std;

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

class中级:public Base {

};

类最后:public Intermediate {
void Message(){
cout< 你好,世界! << endl;
}
};

int main(){
Final final;
/ * Wont work(显然):
Final * finalPtr =& final;
finalPtr-> Message();
* /
//工作:
中级* finalPtr =& final; //或Base * finalPtr =& final;
finalPtr-> Message();

return 0;
}

请注意以下事项:


  1. 在抽象的基础类中,纯虚函数 message()是public

  2. <在类中继承类(也是抽象)继承基类 strong>?)
  3. 最终类继承自中级类和 message


  4. 问题: $ b $ b如果您运行该程序, finalPtr-> Message(); 行成功调用 Final message() 私人。这是怎么回事?一个基类是否重写或忽略一个派生类的访问限制?



    相关问题:上面,定义中级类的正确方法是什么?需要从基类重新声明纯虚函数 message(),同时考虑到中级类并不是为了提供实现。



    注意:代码已经通过Digital Mars Compiler(dmc)和Microsoft的Visual Studio Compiler(cl)测试,并且在

    解决方案


    这是怎么回事?基类是否覆盖或忽略派生类的访问限制?


    使用公共继承,Base类的所有公共成员都将成为公共成员的派生类。因此是 Message()中间体中的公共函数。



    函数在基类( Intermediate )上调用,该函数在基类中是public。动态分派(即对Derived类函数的实际调用)只发生在运行时,因此这是有效的。



    上述是由于在运行时,访问说明符没有意义,访问说明符规则只在编译时解析和有效。



    如果你在派生类指针上调用函数,那么在编译时编译器检测到 Message() Final 中宣告为 private 给出错误。






    派生类 MUST 对于 ALL 的Base虚拟函数,如果不这样做将导致Derived类也是抽象类。



    中级类是一个抽象类,只要你不需要创建这个类的对象,它就会正常工作。请注意,您可以创建一个指向Abstract类的指针。


    Please look at the following code listing:

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

    NOTE: Code was tested with both Digital Mars Compiler (dmc) and Microsoft's Visual Studio Compiler (cl) and works just fine in both

    解决方案

    How does that happen? Does a base class override or ignore a derived class' access restrictions?

    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.

    The function is called on a base class(Intermediate) pointer the function is public in base class. The dynamic dispatch(i.e the actual call to Derived class function) only happens at runtime hence this works.

    The above is due to the fact that at runtime, the access specifiers have no meaning, the access specifier rules are resolved and effective only at compile time.

    If you call the function on the derived class pointer then at compile time the compiler detects that Message() is declared private in Final and hence it gives the error.


    While deriving from an Abstract class, the derived class MUST provide definition for ALL the Pure virtual functions of the Base class, Failing to do so will result in the Derived class also being an Abstract class.

    Here Intermediate class is an Abstract class and as long as you do not need to create objects of this class, it will work fine. Note that you can create a pointer to an Abstract class.

    这篇关于多态和数据隐藏:基类是否重写或忽略派生类的访问限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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