首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

操作符重载有关问题

2012-03-17 
操作符重载问题我定义了个集合类Aggregate,然后重载了 + ,用于集合的并运算。(返回为 const Aggregate 值类

操作符重载问题
我定义了个集合类Aggregate,然后重载了 + ,用于集合的并运算。(返回为 const Aggregate 值类型)
const Aggregate operator + (Aggregate & p);

主函数中 定义Aggregate A,B,C 三个变量
给A,B输入了值.
然后 令C = A+B; 报错。出现内存错误提示框。
  让C = A;也报错
但是,我自己写了个 Aggregate & operator = (Aggregate & p);
就没有错误了,为什么,系统默认的 operator = 为什么不管用呢?
希望知道的人帮一下忙,感激不尽!!!


[解决办法]
GOOGLE /baidu:
深拷贝与浅拷贝
[解决办法]
Aggregate类中都是什么变量 查下
[解决办法]
operator + 写成友元函数是最合适的, 如果你想让它当成员函数, 请写operator +的const版.

friend const Aggregate operator + (const Aggregate &, const Aggregate &)是+运算的标准形式。
Aggregate& operator = (const Aggregate &)是赋值运算的标准形式.

热点排行