cout <<string 引发的思考

// vs 2010
//

#inlcude "stdafx.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
  string str;
  cout <<str <<endl;      // Compilation error.

  return 0;
}
当你写下这样的代码时你将会得到一大堆的编译错误, 其中比较有用的在第一行:  error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'.
意思是说, 没有能够执行 operator<<(const std::string &) 的函数. 
根据我们的常识, 这就像有人突然告诉你世界上再无征税了一样, 显然是有问题的, 那么问题出在了哪里?
记得 Thinking in C++  一书中说过,  将不是属于类的行为作为普通或者友员函数实现. 对于本例而言, 对 string 类的输出不是 string 的行为, 也不是 cout(ostream 的一个对象) 的行为, 因为 cout 不光输出 string 类型, 它还会输出别的很多类型. 因此, 对于 string 的输出, 不应该作为 cout 的一个成员. 所以
cout <<str <<endl; 
是无法通过编译的, 因为 iostream.h 中根本没有对 operator<<(string) 的重载.
根据 C++ Name lookup 约定, cout 函数会进行如下查找:
  • 查找 cout 的命名空间中, 使用点之前是否有 operator<<(std::ostream &, const std::string &) 的声明.
显然, iostream 中并没有相关的函数声明, 然而真正对于 operator<<(std::ostream &, const std::string &) 的声明在头文件 string 中.  将 cout 对 string 的重载放在 string 这个头文件中是正确的设计. 这也是本文阐述的主旨.
因此, 让上述代码工作, 仅需要加入 string 头文件.

转载于:https://www.cnblogs.com/walfud/articles/2318286.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值