2

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

  • 1
    You don't need to inherit from std::ostream for that. You usually overload the function `std::ostream& operator<<(std::ostream& os, MyType const& m);` – Galik May 28 '17 at 20:16
  • This post may be of use for that: https://stackoverflow.com/questions/37720431/what-is-the-c-c-equivalence-of-java-io-serializable/37721075#37721075 – Galik May 28 '17 at 20:20
  • @Galik Normal overloading works. I can then use the constructor, work with my "mout", "min" as well as with standard threads. But in order to pass them as parameters, as well as standard streams, my thread must be inherited. – Yevhenii Nadtochii May 28 '17 at 20:23
  • @Galik * Inherited from them or from the base_stream... – Yevhenii Nadtochii May 28 '17 at 20:25
  • _"`MyStream mout = MyStream();`"_ Pretty sure you just meant `MyStream mout;` And, instead of lots of `...` that could represent almost anything, show your _actual_ [mcve]. – Lightness Races in Orbit May 28 '17 at 23:31
  • You probably don't want to inherit from `istream` or `ostream` at all. If your class is a new data source, you should be subclassing `streambuf`. – user207421 May 29 '17 at 00:39

1 Answers1

2

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>{ ... }

Caleth
  • 52,200
  • 2
  • 44
  • 75