标题:大家帮帮忙,为什么是这个结果
取消只看楼主
惜缘
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2010-7-2
结帖率:100%
已结贴  问题点数:10 回复次数:0 
大家帮帮忙,为什么是这个结果
#include <iostream>

using namespace std;

class  Base
{  public:
Base(int i=0):x(i){}
virtual int sum( ) const
{     return x;     }
private:
    int x;  };
   
    class Derived: public Base
    { public:
    Derived(int i=0,int j=0):Base(i),y(j){}
    int sum( )
    {      return Base::sum()+y;     }
    private:
        int y;  };
        void Call ( Base &b )
  {   cout<<"Sum="<<b.sum()<<endl;   }  

        void main()
        {     Base b(10);
        Derived  d(10,40);
        Call (b);
        Call (d);  }

为什么不加红笔的const,运行结果是:Sum=10 Sum=50 ,而加上红笔的const之后,运行结果是: Sum = 10 Sum =10
2010-12-05 20:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-327998-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.191852 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved