class Shape{
public:
virtual int fun()=0;
};
class Rec:public Shape{
int x,y;
public:
Rec(int a,int b){
x=a;
y=b;
}
void Set(int a,int b){
x=a;
y=b;
}
virtual int fun(){
return x*y;
}
};
void main(){
Rec r1(1,1);
Rec r2(10,10);
Shape &p=r1;
r1=r2;
cout<<p.fun()<<endl; //输出x*y=100
r1.Set(1,1);
cout<<p.fun()<<endl; //输出x*y=1
p=r2;
cout<<p.fun()<<endl; //输出x*y=1,为什么不是输出100?????????
while(1);
}
可是如果对int型进行引用的话,
int a=1,b=2;
int &p=a;
cout<<a<<p<<endl; //输出1,1
p=b;
cout<<a<<p<<endl; //输出2,2
a=3;
cout<<a<<p<<endl; //输出3,3
无论是改变a还是p他们俩都会一起改变,跟上面的类的引用不一样