What are 'aliased' stream buffers?

2019-05-12 14:54发布

What are 'aliased stream buffers`? I encountered the term in a comment on an answer of mine.

3条回答
劫难
2楼-- · 2019-05-12 15:29

What probably was meant in the comment there is this:

ofstream file;
file.rdbuf(cout.rdbuf());

// writes to cout
file << "hello";

So now the check there doesn't work:

if(&file == &cout)
    // no, it doesn't
查看更多
劫难
3楼-- · 2019-05-12 15:30

I've never heard the term before, but in the thread you cite, the person who used it also gave an example: two streams which use the same streambuf.

Of course, just because two streams don't use the same streambuf, doesn't mean that data written to them doesn't ultimately end up in the same place; that they don't alias the same sink, if that is what is meant. There are filtering streambuf's, which forward the actual sinking and sourcing to another streambuf, and on most systems, it's possible to open a file at the system level, and connect a streambuf (or two) to it.

-- James Kanze

查看更多
霸刀☆藐视天下
4楼-- · 2019-05-12 15:47

It means an object with different name, for example this:

ostream &lbw = cout;

lbw << "Shahid out" << "Sachin in" << endl; //goes to cout!
查看更多
登录 后发表回答