test & test::count_price(const test &a){
price+=a.price;
return *this;
}
原本想写成这样:
test &c=a.count_price(b);//获取a的引用
结果意外写成:
test c=a.count_price(b);
编译也能通过,问题就来了,为什么这样做可以呢?引用的实质是什么呢?
test & test::count_price(const test &a){
price+=a.price;
return *this;
}
原本想写成这样:
test &c=a.count_price(b);//获取a的引用
结果意外写成:
test c=a.count_price(b);
编译也能通过,问题就来了,为什么这样做可以呢?引用的实质是什么呢?
你不理解的是因为:引用返回后调用拷贝构造函数生成了对象c
另一个就是正常的引用赋值。