ostream、istream、iostream等頭檔案我們經常在使用,那么這些頭檔案到底做了些什么,我們且看原始碼:
一、ostream頭檔案是宣告了basic_ostream模板類:
#include <ios>
#include <bits/ostream_insert.h>
template<typename _CharT, typename _Traits>
class basic_ostream : virtual public basic_ios<_CharT, _Traits>
{
public:
// 型別 (從basic_ios繼承而來,basic_ios在basic_ios.h中):
typedef _CharT char_type; //_CharT 字符流的型別
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;
typedef _Traits traits_type; //_Traits 字符的型別特征
}
二、istream頭檔案,可以看到istream頭檔案宣告了basic_istream模板類
#include <ios>
#include <ostream>
template<typename _CharT, typename _Traits>
class basic_istream : virtual public basic_ios<_CharT, _Traits>
{
public:
// Types (inherited from basic_ios (27.4.4)):
typedef _CharT char_type; //_CharT 字符流的型別
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;
typedef _Traits traits_type; //_Traits 字符的型別特征
}
三、iostream,只是宣告了一個istream物件和三個ostream物件,該檔案僅有短短幾十行代碼,
我們常用的cin物件就是istream的一個實體,而cout則是ostream的實體:
#include <bits/c++config.h>
#include <ostream>
#include <istream>
namespace std _GLIBCXX_VISIBILITY(default)
{
extern istream cin; /// Linked to standard input
extern ostream cout; /// Linked to standard output
extern ostream cerr; /// Linked to standard error (unbuffered) 不帶緩沖
extern ostream clog; /// Linked to standard error (buffered) 帶緩沖
// 用于構造檔案緩沖區,給cout, cin, cerr, clog et. al.
static ios_base::Init __ioinit;
}
四、iosfwd頭檔案
下面是iosfwd的一個片段,可以看到該頭檔案里面宣告了所有輸入輸出類的模板類的一個實體:
namespace std _GLIBCXX_VISIBILITY(default)
{
class ios_base;
template<typename _CharT, typename _Traits = char_traits<_CharT> >
class basic_ios;
template<typename _CharT, typename _Traits = char_traits<_CharT> >
class basic_istream;
template<typename _CharT, typename _Traits = char_traits<_CharT> >
class basic_ostream;
template<typename _CharT, typename _Traits = char_traits<_CharT> >
class basic_iostream;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/303616.html
標籤:其他
上一篇:iOS單例模式寫法以及GCD
