鱼C论坛

 找回密码
 立即注册
查看: 1344|回复: 5

[已解决]cout自动识别

[复制链接]
发表于 2018-9-2 19:39:03 | 显示全部楼层 |阅读模式

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

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

x
最近心血来潮自学C++  奈何碰到一些问题



我这样使用cout是错误的吗,我知道 如果把   int ch; 改成 char ch; 就会运行正确的输出结果, 但是   getchar 函数返回的是整型, 整型要求我去赋值给字符型(我才不干嘞!) 于是乎我考虑了一个问题,cout应该具有自动识别数据类型的功能吧,不然怎么能做到不格式化就可以输出呢?   况且,在我看来,char 只不过是八位的整型罢了.  一家人才对! 有没有精通(滑稽)C++的大佬指点迷津哇!?
  1. __CRT_INLINE __cdecl __MINGW_NOTHROW  int getchar (void)
复制代码
最佳答案
2018-9-3 00:16:03
  1. #include <iostream>

  2. namespace Test
  3. {
  4.         class ostream
  5.         {
  6.         public:
  7.                 ostream &operator<<(int num)
  8.                 {
  9.                         printf("%d", num);
  10.                         return *this;
  11.                 }
  12.                 ostream &operator<<(char ch)
  13.                 {
  14.                         putchar(ch);
  15.                         return *this;
  16.                 }
  17.                 ostream &operator<<(ostream &(*pf)(ostream &out))
  18.                 {
  19.                         return pf(*this);
  20.                 }       
  21.         };

  22.         ostream cout;

  23.         ostream &endl(ostream &out)
  24.         {
  25.                 putchar('\n');
  26.                 fflush(stdout);
  27.                 return out;
  28.         }
  29. };

  30. int main()
  31. {
  32.         int ch1 = 'a';
  33.         char ch2 = 'a';
  34.         Test::cout << ch1 << Test::endl;
  35.         Test::cout << ch2 << Test::endl;

  36.         return 0;
  37. }
复制代码

  1. 97
  2. a
  3. 请按任意键继续. . .
复制代码



Test::cout << ch1 << Test::endl;    // 调用的是 ostream &operator<<(int num)

Test::cout << ch2 << Test::endl;    // 调用的是 ostream &operator<<(char ch)
2018-09-02 (3).png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-9-2 20:03:53 | 显示全部楼层
我想了一会儿,觉得这样也是很合理的,至于getchar为何会设计成返回整型,C陷阱和缺陷第八十四页有写。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-2 20:37:06 | 显示全部楼层
char ch;
(ch = (char)getchar() != '\n')  ??
EOF 的关系

iostream 都载了…… int 跟 iostream 的大小完全不能比~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-2 20:52:47 | 显示全部楼层
本帖最后由 moc 于 2018-9-2 20:54 编辑

ostream& operator<<(ostream &out, 类名 &name)     
cout<<可以这样重载的,这样你定义的新的类都可以通过这种方式,把你想要打印出来的东西打印下来。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-2 21:07:42 | 显示全部楼层

确定类型的关键字是哪个。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-3 00:16:03 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>

  2. namespace Test
  3. {
  4.         class ostream
  5.         {
  6.         public:
  7.                 ostream &operator<<(int num)
  8.                 {
  9.                         printf("%d", num);
  10.                         return *this;
  11.                 }
  12.                 ostream &operator<<(char ch)
  13.                 {
  14.                         putchar(ch);
  15.                         return *this;
  16.                 }
  17.                 ostream &operator<<(ostream &(*pf)(ostream &out))
  18.                 {
  19.                         return pf(*this);
  20.                 }       
  21.         };

  22.         ostream cout;

  23.         ostream &endl(ostream &out)
  24.         {
  25.                 putchar('\n');
  26.                 fflush(stdout);
  27.                 return out;
  28.         }
  29. };

  30. int main()
  31. {
  32.         int ch1 = 'a';
  33.         char ch2 = 'a';
  34.         Test::cout << ch1 << Test::endl;
  35.         Test::cout << ch2 << Test::endl;

  36.         return 0;
  37. }
复制代码

  1. 97
  2. a
  3. 请按任意键继续. . .
复制代码



Test::cout << ch1 << Test::endl;    // 调用的是 ostream &operator<<(int num)

Test::cout << ch2 << Test::endl;    // 调用的是 ostream &operator<<(char ch)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 09:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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