this->init(0); what does it mean

Hi guys I tried to make an instance of an ostream class but when I did I got an error and it brought me to the ostream itself,not sure if it's the ostream.cpp file or the header,it just says ostream anyway,

this block of code kind of confuses me,I have not seen it before.

1
2
3
4
 protected:
      basic_ostream()
      { this->init(0); }


I'm guessing basic_ostream() is a #define or a typedef for ostream because the class name is ostream not basic_ostream,secondly by making it protected only classes that inherit from basic_ostream() can create an instance of it?

and finally the part that confuses me the the most what is the init function and why does it take 0 as an argument?


**edit another question

1
2
ostream out(nullptr);


how come when I pass a nullptr to the constructor it will compile fine?

thanks
Last edited on
You know how when you make your own files, you #include "foobar.h"? Well, "ostream" is the literal name of the file in this case. It just doesn't have an extension.

The line of code you are referring to is within the class declaration for basic_ostream, so basic_ostream() is the no-arg constructor. And yes, you as a user cannot do ostream os; because the default constructor is protected.

The details are a bit hairy, but you're basically correct: basic_ostream<typename _CharT, typename _Traits> is a class template that inherits from basic_ios<_CharT, _Traits>, with _Traits defaulting to char_traits<_CharT>.

ostream is an instantiation of this class template, defined as
typedef basic_ostream<char> ostream;
This is most likely in your iosfwd file.


I don't know what the init function is specifically, that's just some implementation-defined detail.


_______________________________

ostream out(nullptr);

This is calling explicit ostream (streambuf* sb);
http://www.cplusplus.com/reference/ostream/ostream/ostream/

Note:
Objects of class ostream are seldom constructed directly. Generally some derived class is used (like the standard ofstream and ostringstream).
Last edited on
In other words, you cannot create an 'ostream'; it is an abstract superclass that describes what all kinds of output streams should do.

You create concrete objects like ostringstream or ofstream, which inherit from ostream. You can then use a reference to an ostream to perform operations on the concrete object without caring what kind of stream it actually is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

void say_hello( std::ostream& outs )  // takes any output stream
{
  outs << "Hello!\n";
}

int main()
{
  // standard output
  say_hello( std::cout );

  // create and output to a named file
  std::ofstream f( "fooey.txt" );
  say_hello( f );

  // write to a string
  std::ostringstream ss;
  say_hello( ss );
  std::string s = ss.str();
  std::cout << "string says: " << s;
}

If you create your own output stream type, you will also need to inherit from ostream.

Hope this helps.
Topic archived. No new replies allowed.