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

时间:2021-06-08 12:38:54
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.

cout,cerr和clog被声明为ostream的类型。但是cout和clog有缓冲,但cerr没有。

How come the same type of objects behaves differently?

为什么相同类型的对象表现不同?

2 个解决方案

#1


2  

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.

这三个对象具有相同的类型,但它们不是同一个实例。 ostream的实例可以配置不同;在这种情况下,三个对象被配置为以其特定方式处理每个对象。这是有道理的:如果同一个类的所有对象都以完全相同的方式运行,那么就没有理由首先将它们作为单独的对象。

#2


2  

The constructor takes a streambuf option

构造函数采用streambuf选项

explicit ostream (streambuf * sb);

显式ostream(streambuf * sb);

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

我猜他们每个人使用不同的streambuf输出,那些streambuf对象控制输出是如何缓冲的(或不是)。

#1


2  

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.

这三个对象具有相同的类型,但它们不是同一个实例。 ostream的实例可以配置不同;在这种情况下,三个对象被配置为以其特定方式处理每个对象。这是有道理的:如果同一个类的所有对象都以完全相同的方式运行,那么就没有理由首先将它们作为单独的对象。

#2


2  

The constructor takes a streambuf option

构造函数采用streambuf选项

explicit ostream (streambuf * sb);

显式ostream(streambuf * sb);

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

我猜他们每个人使用不同的streambuf输出,那些streambuf对象控制输出是如何缓冲的(或不是)。