千里之行,始于足下

《effective c++ II》学习笔记 Item 2 Prefer iostream to stdio.h

关键词:safety, extensibility

int i;
Rational r;                           
// r is a rational number

cin 
>> i >> r;
cout 
<< i << r;


class Rational {
public:
  Rational(
int numerator = 0int denominator = 1);
  
private:
  
int n, d;    // numerator and denominator
friend ostream& operator<<(ostream& s, const Rational& r);
};
ostream
& operator<<(ostream& s, const Rational& r)
{
  s 
<< r.n << '/' << r.d;
  
return s;
}

绕开scanf(),printf()恼人的格式要求。使用便捷,安全的<<,>>操作符。

最后,
#include<iostream.h>是全局意义上的库函数声明,容易冲突。
#include<iostream>是STL的写法,要名字空间std::声明。


posted on 2007-12-10 16:12 rednight 阅读(218) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理