7

How can I derive my own stream from a standard stream?

In C# language, there is a Stream class, but C++'s streams are too complex.

I want something like this:

class my_stream : public std::stream
{
  // How to derive?
};

void using_a_stream(std::stream* s)
{
  *s << "Hello world";
}

void main()
{
  std::stream s1;
  std::fstream s2("C:\\test.txt");
  my_stream s3;

  using_a_stream(&s1);
  using_a_stream(&s2);
  using_a_stream(&s3);
}

Note: The code just a sample and may be invalid C++ program. Thanks.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
  • 4
    Publically inheriting from the STL classes is strongly discouraged. Why can't you just use the class as-is? – Cody Gray - on strike Jun 27 '11 at 08:56
  • 7
    @Cody Gray: What you say is true for some of the classes. However streams and streambuf classes are made/prepared to be extended by inheritance. – wilx Jun 27 '11 at 09:04
  • @Cody: I develope a library and I should derive my own stream to replace some STL strams. – Amir Saniyan Jun 27 '11 at 09:40
  • That doesn't actually answer the question. *Why* do you need to derive your own stream to replace the STL streams? Yes, wilx is right: the stream and streambuf classes are actually designed for inheritance. But it's fairly complicated, and there's rarely a good reason for it. You don't need to derive your own stream just so it uses your own name. – Cody Gray - on strike Jun 27 '11 at 10:16
  • 1
    It is not an STL stream. The STL comprises the containers library, algorithms library, and iterators, which are used to connect those two libraries. IOStreams is completely separate. I have edited the question accordingly. – Billy ONeal Jun 27 '11 at 11:05

3 Answers3

15

I think there are three levels of answer to this question:

Level 1: It is complicated, especially if you are completely new to C++, stop right now. Only if you feel adventurous, continue to level 2.

Level 2: Use some library that makes creating streams easier. I would suggest using Boost.IOStreams library. It makes creating own streams and streambufs much easier. If you are still not satisfied, continue to level 3.

Level 3: You will have to derive from std::streambuf and modify its behaviour to suit your needs. Then you will have to plug your streambuf into own stream.

wilx
  • 17,697
  • 6
  • 59
  • 114
  • 6
    That's probably the correct answer (except for level 1: it's really not very difficult to derive from `std::streambuf`). But we can't be sure until we know why he wants to derive from `std::stream` (and why from `std::stream`, rather than `std::istream` and `std::ostream`). Another possible solution would be a class which contains an `std::stream`, with template `operator<<` and `operator>>` to forward to it. Which solution to use depends on what you are trying to do. – James Kanze Jun 27 '11 at 10:55
  • +1 -- one should not really derive from a stream, one should derive from a streambuf. – Billy ONeal Jun 27 '11 at 11:08
  • @BillyONeal: In the end you need both. You derive from `streambuf` to get the thing working and then you often derive from `stream` to provide a convenient way of instantiating your `streambuf`. – wilx Aug 23 '14 at 17:32
0

Could you please describe a little bit more what you own streamclass should do? Just asking how without what is not the best way to get a constructive answer.

Maybe you should have a look at boost::iostream, as there is a much simpler and safer way to write own iostream classes.

Thomas Berger
  • 1,860
  • 13
  • 26
-7

Don't.

iostreams is an awful interface. It also lacks a lot of features and has awful performance.

Community
  • 1
  • 1
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • 6
    -1: If you're going to denigrate iostreams, you could at least provide evidence for your assertions. Or failing that, provide an alternative mechanism that he could use. – Nicol Bolas Jun 27 '11 at 09:17
  • 4
    -1. Not useful or really relevant to the OP. `printf` is not generic, and you can't extend it at all — so it fails the basic requirement. Not to mention it's type-unsafe. – Cat Plus Plus Jun 27 '11 at 09:30
  • 1
    -1. Asker wanted ABC not an explanation why XYZ is better. Then your main points about XYZ are wrong. Streams have more, but admittetly odd, features and performance is implementation dependent. On most compilers I have checked streams are faster. On msvc performance is as you describe. – Sqeaky Sep 03 '15 at 20:00