藤井のスタートページ

C++のiostream

C++のiostreamの感想など、、

C++ iostream の実装

TCP/IP で connect した socket を iostream 化する実装例を示す。

実装にあたって規格を読んでの考察や規格の抜粋、(私の)解釈を示す。

その他、雑メモ

大雑把に言うと

  • iostream(istream、ostream も)は基本的に 入出力を streambuf で行っている。つまり入出力は実はstreambuf が本体。
  • streambuf と iostream を結びつけるのは iostream の constractor。 iostream の constractor は streambuf object の pointer を引数に持つ。

実際には streambuf も iostream も template なので、

template <class Ch, class Tr=std::char_traits<Ch> >
class basic_mystrembuf : basic_streambuf<Ch, Tr>
{
public:
  basic_mystream()
  virtual ~basic_mystream();
      :
};
template <class Ch, class Tr=std::char_traits<Ch> >
class basic_mystream : public std::basic_iostream<Ch,Tr>
{
public:  
  basic_mystream() 
       : std::basic_iostream<Ch,Tr>(new basic_mystreambuf<Ch,Tr>())
  {
  }

  ~basic_mystream()
  {
  }
};
typedef basic_mystreambuf<char> mystreambuf;
typedef basic_mystream<char> mystream;

とすればよいらしい。でも fstream を覗いてみると、ifstream と ofstream も 作っているようだ。

C++でiostreamを拡張したい時の参考 URL。当初、参考にしよう としたが、結局は規格を調べざるを得なかった(上記「実証資料」の項 参照)。


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2010-11-19 (金) 10:11:45