- 论坛徽章:
- 0
|
真不好意思,先前一个程序我弄错了
不过又有一个简单的问题想请教大家.....
如果没有定义构造函数,编译器将生成一个缺省的构造函数和一个拷贝构造函数。这句话正确吗?如果正确为什么会生成一个拷贝构造函数呢?
#include<iostream>//重载运算符+=的程序分析
using namespace std;
class Amount{
int amount;
public:
Amount(int n=0):amount(n){};
int getAmount()const {return amount;}
Amount &operator+=(Amount a){amount+=a.amount; return *this;}//为什么要有(Amount a)呢?这是必须要有的,否则就无法编译
};//return this和return *this 有什么不同啊?,这里return this也无法编译,所以我不明白两者的区别
int main()
{
Amount x(3),y(7);
x+=y;
cout<<x.getAmount()<<endl;
return 0;
}
[ 本帖最后由 中国可爱小牛 于 2009-7-2 22:44 编辑 ] |
|