ramsse
ramsse

Reputation: 23

How the same type of objects cout, cerr, clog perform differently?

extern ostream cout;
extern ostream cerr;
extern ostream clog;

cout, cerr and clog are declared the type of ostream. But cout and clog have buffers but cerr does not.

How come the same type of objects behaves differently?

Upvotes: 2

Views: 166

Answers (2)

Karthik T
Karthik T

Reputation: 31952

The constructor takes a streambuf option

explicit ostream (streambuf * sb);

Im guessing each of them use different streambuf outputs, and those streambuf objects control how the output is buffered(or not).

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

The three objects have the same type, but they are not the same instance. Instances of ostream could be configured differently; in this case, the three objects are configured to deal with buffering each in its specific way. This makes sense: if all objects of the same class behaved in the same exact way, there would be no reason to make them separate objects in the first place.

Upvotes: 2

Related Questions