鱼C论坛

 找回密码
 立即注册
查看: 984|回复: 3

[已解决]关于重载C++的<<运算符

[复制链接]
发表于 2020-4-23 15:33:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 析觇啊 于 2020-4-23 23:02 编辑
  1. #include <iostream>

  2. class MyInt
  3. {
  4.         friend std::ostream& operator << (std::ostream& out, const MyInt &myint);

  5. public:
  6.         MyInt()
  7.         {
  8.                 m_num = 0;
  9.         }

  10.         MyInt& operator++ ();
  11.         MyInt operator++(int);

  12. private:
  13.         int m_num;
  14. };

  15. MyInt& MyInt::operator++()
  16. {
  17.         this->m_num += 1;
  18.         return *this;
  19. }

  20. MyInt MyInt::operator++(int)
  21. {
  22.         MyInt temp = *this;
  23.         this->m_num++;
  24.         return temp;
  25. }

  26. std::ostream& operator << (std::ostream& out, const MyInt &myint)
  27. {
  28.         out << myint.m_num;
  29.         return out;
  30. }

  31. int main()
  32. {
  33.         MyInt myint;
  34.         std::cout << (++(++myint)) << std::endl;

  35.         std::cout << myint++ << std::endl;


  36.         //myint++;
  37.         //std::cout << myint << std::endl;
  38. }
复制代码
请问这段代码哪里有问题呢。我重载后置++后确实是返回MyInt类了呀。
而且为什么如果我按注释的46和47行就可以成功输出呢。谢谢大家!
最佳答案
2020-4-23 16:21:25

你在 你的帖子下面回复我,我是没有通知的,除非我点进来才能知道你回复了我

  1. #include <iostream>

  2. class MyInt
  3. {
  4.         friend std::ostream& operator << (std::ostream& out, const MyInt &myint);

  5. public:
  6.         MyInt()
  7.         {
  8.                 m_num = 0;
  9.         }

  10.         MyInt& operator++ ();
  11.         MyInt operator++(int);

  12. private:
  13.         int m_num;
  14. };

  15. MyInt& MyInt::operator++()
  16. {
  17.         this->m_num += 1;
  18.         return *this;
  19. }

  20. MyInt MyInt::operator++(int)
  21. {
  22.         MyInt temp = *this;
  23.         this->m_num++;
  24.         return temp;
  25. }

  26. std::ostream& operator << (std::ostream& out, const MyInt &myint)
  27. {
  28.         out << myint.m_num;
  29.         return out;
  30. }

  31. int main()
  32. {
  33.         MyInt myint;
  34.         std::cout << (++(++myint)) << std::endl;

  35.         std::cout << myint++ << std::endl;


  36.         //myint++;
  37.         //std::cout << myint << std::endl;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-23 15:34:50 | 显示全部楼层
把代码以文本的形式贴上来,我需要复制粘贴,而不是看着图片抄一遍
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 16:21:25 | 显示全部楼层    本楼为最佳答案   

你在 你的帖子下面回复我,我是没有通知的,除非我点进来才能知道你回复了我

  1. #include <iostream>

  2. class MyInt
  3. {
  4.         friend std::ostream& operator << (std::ostream& out, const MyInt &myint);

  5. public:
  6.         MyInt()
  7.         {
  8.                 m_num = 0;
  9.         }

  10.         MyInt& operator++ ();
  11.         MyInt operator++(int);

  12. private:
  13.         int m_num;
  14. };

  15. MyInt& MyInt::operator++()
  16. {
  17.         this->m_num += 1;
  18.         return *this;
  19. }

  20. MyInt MyInt::operator++(int)
  21. {
  22.         MyInt temp = *this;
  23.         this->m_num++;
  24.         return temp;
  25. }

  26. std::ostream& operator << (std::ostream& out, const MyInt &myint)
  27. {
  28.         out << myint.m_num;
  29.         return out;
  30. }

  31. int main()
  32. {
  33.         MyInt myint;
  34.         std::cout << (++(++myint)) << std::endl;

  35.         std::cout << myint++ << std::endl;


  36.         //myint++;
  37.         //std::cout << myint << std::endl;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 16:39:00 | 显示全部楼层
人造人 发表于 2020-4-23 16:21
你在 你的帖子下面回复我,我是没有通知的,除非我点进来才能知道你回复了我

不好意思哦,第一次提问,还不太会使用。谢谢提醒
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 12:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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