Standard iostreams ---Type safety

Let us compare a call to stdio functions with the use of standard iostreams. The stdio call reads as follows:

让我们比较调用"C stdio"函数与使用标准iostreams 。用"C Stdio" “读”,如下:

int i = 25;
char name[50] = "Janakiraman";
fprintf(stdout, "%d %s", i, name);

It correctly prints: 25 Janakiraman.

正确地打印是:  25 Janakiraman

But what if we inadvertently switch the arguments to fprintf? The error will be detected no sooner than run time. Anything can happen, from peculiar output to a system crash. This is not the case with the standard iostreams:

但如果我们不经意地交换了参数 fprintf 会发生什么?这个错误一运行就能觉察到。从奇怪的输出到系统崩溃任何事都可能发生。标准 iostreams不会发生这样的事:
 
cout << i << ' ' << name << '/n';

Since there are overloaded versions of the shift operator operator<<(), the right operator will always be called. The function cout << i calls operator<<(int), and cout << name calls operator<<(const char*). Hence, the standard iostreams are typesafe.

因为重载的"shift "操作符 operator<<() 始终都调用正确的操作。cout << i 调用函数 operator<<(int),cout << name 调用函数  operator<<(const char*)。因此, 标准iostreams 是类型安全的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值