鱼C论坛

 找回密码
 立即注册
查看: 4040|回复: 8

关于操作符重载,球解答

[复制链接]
发表于 2013-7-6 15:10:53 | 显示全部楼层 |阅读模式
3鱼币
本帖最后由 wric 于 2013-7-9 16:58 编辑

问题1:为什么我重载后的“<< ”操作符会调用该类的副构造函数。没把副构造函数注释掉,输出是这样的:
vice-constructor
5
问题2:当我用&rhs代替rhs时为什么会出错?为什么?
27行,std::ostream& operator<<(std::ostream& os,MyClass rhs)

最佳答案

查看完整内容

1. 因为MyClass不是传引用,而是传址操作,当然涉及到对象的复制,而调用副本构造器 2.每一个操作符重载都有固定的规范,不可以随意更改 微软官网写的清清楚楚的其中一个参数必须为对象(基本数据类型也算是对象),不可为引用
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-6 15:10:54 | 显示全部楼层
本帖最后由 565123 于 2013-7-7 20:06 编辑

1. 因为MyClass不是传引用,而是传址操作,当然涉及到对象的复制,而调用副本构造器
2.每一个操作符重载都有固定的规范,不可以随意更改

ret-type  operatorop (  arg1 ,  arg2  )

where ret-type and op are as described for member operator functions and arg1 and arg2 are arguments. At least one of the arguments must be of class type.

微软官网写的清清楚楚的其中一个参数必须为对象(基本数据类型也算是对象),不可为引用





小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-6 15:13:27 | 显示全部楼层
代码如下:
  1. #include <iostream>
  2. class MyClass
  3. {
  4. public:
  5.     MyClass(int *x);
  6.     MyClass(const MyClass &rhs);

  7. private:
  8.     int *p;
  9.     friend std::ostream& operator<<(std::ostream& os,MyClass iClass);
  10. };

  11. MyClass::MyClass(int *x)
  12. {
  13.     p = x;
  14. }

  15. #if 1
  16. MyClass::MyClass(const MyClass &rhs)
  17. {
  18.     std::cout << "vice-constructor\n";
  19.     *this = rhs;
  20. }
  21. #endif

  22. ///problem one,if I use &rhs instead of rhs,it will prompt an error
  23. std::ostream& operator<<(std::ostream& os,MyClass rhs)
  24. {
  25.     os << *rhs.p << "\n";
  26.     return os;
  27. }

  28. int main()
  29. {
  30.     MyClass i1( new int(5) );
  31.     ///problem two,if I defined a vice-constructor,this override
  32.     /// "<<" operator will call my vice-constructor.why?
  33.     std::cout << i1;

  34.     return 0;
  35. }


复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-6 15:15:20 | 显示全部楼层
不懂啊,球好心人解答
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-6 19:49:00 | 显示全部楼层
强烈支持楼主,不会同问
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-7 22:57:26 | 显示全部楼层
  1. #include <iostream>

  2. //加上这两行
  3. class  MyClass;
  4. std::ostream& operator<<(std::ostream& os,MyClass& iClass);


  5. class MyClass
  6. {
  7. public:

  8.     MyClass(int *x);

  9.     MyClass(const MyClass &rhs);


  10. private:

  11.     int *p;

  12.     friend std::ostream& operator<<(std::ostream& os,MyClass& iClass);

  13. };


  14. MyClass::MyClass(int *x)

  15. {

  16.     p = x;

  17. }


  18. #if 1

  19. MyClass::MyClass(const MyClass &rhs)

  20. {

  21.     std::cout << "vice-constructor\n";

  22.     *this = rhs;

  23. }

  24. #endif


  25. ///problem one,if I use &rhs instead of rhs,it will prompt an error

  26. std::ostream& operator<<(std::ostream& os,MyClass& rhs)

  27. {

  28.     os << *rhs.p << "\n";

  29.     return os;

  30. }


  31. int main()

  32. {

  33.     MyClass i1( new int(5) );

  34.     ///problem two,if I defined a vice-constructor,this override

  35.     /// "<<" operator will call my vice-constructor.why?

  36.     std::cout << i1;


  37.     return 0;

  38. }
复制代码
第一个问题,已经有人说了,我就不说了。第二个问题,你的代码一点问题都没有,出问题的是VC6的编译器。如果使用搞高版本的编译器(VS2003及以上)你的代码就是正确的。如果非要试用报告VC6,则可按照上面的代码在开头增加两行。事实上VC6里友元函数基本都要加类似的这两行(谁让VC6不符合标准呢)。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-7 22:59:40 | 显示全部楼层
顺便说下,565123对于2的解释是错误的。其中,At least one of the arguments must be of class type.
是说两个参数不能都是基本类型。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-9 16:53:24 | 显示全部楼层
本帖最后由 wric 于 2013-7-9 17:09 编辑

@小号4
把鱼油的回答与自己Google的总结:

1.一般有三种情况会使用到复制构造函数
a.用同类对象对新对象初始化,如MyClas a;MyClass b(a);
b.对象作为函数的参数时,这时调用此函数时使用的是值传递,也会产生对象的复制。问题一就是这种情况(调用了我自定义的复制构造函数)
c.当函数的返回值是类的对象时

2.操作符重载的参数至少有一个是非基本类型(操作符重载手册: http://www.adintr.com/myarticle/operator.html#z1 )

谢了 @565123    @仰望天上的光
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-9 18:58:01 | 显示全部楼层
老外在C++中将基本数据类型和对象都看做对象了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-20 01:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表