Name

ios_base::Init class — Initialization class

Synopsis

class ios_base::Init {
public:
  Init(  );
  ~Init(  );
};

The Init class is used to ensure that the construction of the standard I/O stream objects occurs. The first time an ios_base::Init object is constructed, it constructs and initializes cin, cout, cerr, clog, wcin, wcout, wcerr, and wclog. A static counter keeps track of the number of times ios_base::Init is constructed and destroyed. When the last instance is destroyed, flush( ) is called for cout, cerr, clog, wcout, wcerr, and wclog.

For example, suppose a program constructs a static object, and the constructor prints a warning to cerr if certain conditions hold. To ensure that cerr is properly initialized and ready to receive output, declare an ios_base::Init object before your static object, as shown in Example 13-15.

Example

Example 13-15. Ensuring proper initialization of standard I/O streams
class myclass {
public:
  myclass(  ) {
    if (! okay(  ))
      std::cerr << "Oops: not okay!\n";
  }
};
static std::ios_base::Init init;
static myclass myobject;

See Also

<iostream>

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.