c++ 输入输出流关联

#include
#include
#include
#include
using namespace std;
int main()
{

  cout << cin.tie()  << endl;  //打印cin的关联流的地址
  cout << &cout << endl;    //打印cout的地址
  cout << &cin << endl;      //打印地址
  cout << cout.tie() << endl;   //打印关联流地址

  cout <<"tie the cerr to cout " << endl;
  cout.tie(&cerr);            //把cerr关联到cout
  cout << cout.tie() << endl;    //打印cout的关联流,即打印cerr的地址
  cout << &cerr <   cout.tie(nullptr);                    //彻底解开关联流,关联流都被解开了把?
  cout << "free the tie:"<

  cout.tie(nullptr);       //解开cout 的关联流
  cout << cout.tie() <   cout << "tie the new:" <   ostream * old_tie  = cin.tie(nullptr);  //解开cin的挂链流,并且把cin原来的关联流返回
  cout << "old value: "<<  old_tie  << endl;        //
  cout << "now 's cin ' s tie is:" <

    return 0;
  }
几个总结:

(1)无论是关联还是解开,都是被关联的流作为主要职责。即调用tie的那个流作为主要的行使权利,它如果要其它流关联到它,那么会调用tie函数,strm.tie(& strm2)          把strm2关联到strm上

(2)如果要解开流的关联,那么strm调用tie来解开。strm.tie(nullptr)则可以了。

(3)谁调用关联,那么谁就调用tie(nullptr)去解开关联。并且这个流如果执行,那么被关联流strm2会首先清除缓存

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部