大家帮帮忙,为什么是这个结果
#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