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

前端 未结 3 1173
予麋鹿
予麋鹿 2021-01-12 21:41

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 thi

相关标签:
3条回答
  • 2021-01-12 22:18

    Don't.

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

    • Are the C formatted I/O functions (printf, sprintf, etc) more popular than IOStream, and if so, why?
    • What are the bad habits of C programmers starting to write C++?
    • Should I use printf in my C++ code?
    • Is it bad practice to use C features in C++?
    • Partially truncating a stream (fstream or ofstream) in C++
    0 讨论(0)
  • 2021-01-12 22:25

    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.

    0 讨论(0)
  • 2021-01-12 22:36

    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.

    0 讨论(0)
提交回复
热议问题