• 一些疑问


    看到书上的代码之后,产生了一些疑问,还请大佬指教一下
    程序如下:

    #include<iostream>
    using namespace std;
    class Base1{
    	public:
    		virtual void display()const;
    };
    void Base1::display()const{
    	cout<<"Base1::display()"<<endl;
    }
    class Base2:public Base1{
    	public:
    		void display()const;
    };
    void Base2::display()const{
    	cout<<"Base2::display()"<<endl;
    }
    class Derived:public Base2{
    	public:
    		void display()const;
    };
    void Derived::display()const{
    	cout<<"Derived::display()"<<endl;
    }
    void fun(Base1 *ptr){
    	ptr->display();
    }
    int main()
    {
    	Base1 base1;
    	Base2 base2;
    	Derived derived;
    	fun(&base1);
    	fun(&base2);
    	fun(&derived);
    	return 0;
    }
    

    这是书上讲虚函数的时候的那道例题
    ………………………………………………………………………………………………………………………………………………………………………………………………………………
    第一个疑问:
    我把虚函数去掉,想试用一下赋值兼容规则,程序变成了下面的样子

    #include<iostream>
    using namespace std;
    class Base1{
    	public:
    		 void display()const;
    };
    void Base1::display()const{
    	cout<<"Base1::display()"<<endl;
    }
    class Base2:public Base1{
    	public:
    		void display()const;
    };
    void Base2::display()const{
    	cout<<"Base2::display()"<<endl;
    }
    class Derived:public Base2{
    	public:
    		void display()const;
    };
    void Derived::display()const{
    	cout<<"Derived::display()"<<endl;
    }
    void fun(Base1 *ptr){
    	ptr->display();
    }
    int main()
    {
    	Base1 base1;
    	Base2 base2;
    	Derived derived;
    	fun(&base1);
    	fun(&base2);
    	fun(&derived);
    	return 0;
    }
    

    但是呐,运行结果不尽人意

    因为呐,书上将赋值兼容规则的时候是把display()声明为内联函数的,这里我就不是很明白为什么会这样了……还请大佬指导一下
    ……………………………………………………………………………………………………………………………………………………………………………………………………
    第二个问题
    我把第12行和14行的const去掉了

    #include<iostream>
    using namespace std;
    class Base1{
    	public:
    		virtual void display()const;
    };
    void Base1::display()const{
    	cout<<"Base1::display()"<<endl;
    }
    class Base2:public Base1{
    	public:
    		virtual void display ();
    };
    void Base2::display(){
    	cout<<"Base2::display()"<<endl;
    }
    class Derived:public Base2{
    	public:
    		void display()const ;
    };
    void Derived::display()const{
    	cout<<"Derived::display()"<<endl;
    }
    void fun(Base1 *ptr){
    	ptr->display();
    }
    int main()
    {
    	Base1 base1;
    	Base2 base2;
    	Derived derived;
    	fun(&base1);
    	fun(&base2);
    	fun(&derived);
    	return 0;
    }
    

    结果就变化了……这个也请大佬讲解一下

  • 相关阅读:
    SpringBoot2.0之一 新建项目helloWorld
    spring boot 的maven设置阿里云仓库
    新建SpringBoot项目运行页面报错Whitelabel Error Page This application has no explicit mapping for /error, so yo
    SpringBoot2.0 最简单的 idea 快速创建项目
    postgresql 按日期范围查询
    postgreSQL 应用case when的例子
    PostgreSQL 数据库NULL值的默认排序行为与查询、索引定义规范
    ASP.NET中在不同的子域中共享Session
    YKCW6-BPFPF-BT8C9-7DCTH-QXGWCYQ7PR-QTHDM-HCBCV-9GKGG-TB2TM
    Asp.net中基于Forms验证的角色验证授权
  • 原文地址:https://www.cnblogs.com/Nicholastwo/p/9157748.html
Copyright © 2020-2023  润新知