Yevhenii Nadtochii
Yevhenii Nadtochii

Reputation: 704

How to correctly inherit my class from istream, ostream?

Well, in general, the goal is such that in the function it is possible to transfer either the object of my class, or cout | cin.

MyStream mout = MyStream();
MyStream min = MyStream();
...
static int UShowTFileList(ostream& out, istream& in);
...
UShowTFileList(cout, cin);
UShowTFileList(mout,min);

The obvious solution does not work. There are no constructors.

class MyStream : public ostream, public istream {...}
...
MyStream mout = MyStream();
MyStream min = MyStream();
...
-->
Error (active)  E1790   the default constructor of "MyStream" cannot be referenced -- it is a deleted function

Well, all the challenges, too.

mout << "Hello, world!" << "\n";
->
Error   C2280   'MyStream::MyStream(const MyStream &)': attempting to reference a deleted function

In general, how correctly to inherit istream, ostream? MyStream.h

Upvotes: 2

Views: 1956

Answers (1)

Caleth
Caleth

Reputation: 62616

The problem you are encountering is there is no nullary constructor for either std::istream nor std::ostream, they both require a std::streambuf* parameter. Therefore Mystream can't have a defaulted constructor, you will have to write one. std::fstream and std::stringstream do this with related subclasses of std::streambuf, std::filebuf and std::stringbuf. I suggest that you provide a streambuf subclass also.

Note that you may as well inherit from std::iostream, which is already a combined input/output stream. Note also that all of these names I have mentioned are type aliases for std::basic_*<char, std::char_traits<char>>, you can easily generalise to template <Char = char, Traits = std::char_traits<Char>> class MyStream : std::basic_iostream<Char, Traits>{ ... }

Upvotes: 2

Related Questions