Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 596821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:12:48+00:00 2026-05-13T16:12:48+00:00

I want to define a class MyStream so that: MyStream myStream; myStream << 1

  • 0

I want to define a class MyStream so that:

MyStream myStream;
myStream << 1 << 2 << 3 << std::endl << 5 << 6 << std::endl << 7 << 8 << std::endl;

gives output

[blah]123
[blah]56
[blah]78

Basically, I want a “[blah]” inserted at the front, then inserted after every non terminating std::endl?

The difficulty here is NOT the logic management, but detecting and overloading the handling of std::endl. Is there an elegant way to do this?

Thanks!

EDIT: I don’t need advice on logic management. I need to know how to detect/overload printing of std::endl.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:12:49+00:00Added an answer on May 13, 2026 at 4:12 pm

    What you need to do is write your own stream buffer: When the stream buffer is flushed you output you prefix characters and the content of the stream.

    The following works because std::endl causes the following.

    1. Add '\n' to the stream.

    2. Calls flush() on the stream

    3. This calls pubsync() on the stream buffer.

      1. This calls the virtual method sync()
      2. Override this virtual method to do the work you want.
    #include <iostream>
    #include <sstream>
    
    class MyStream: public std::ostream
    {
        // Write a stream buffer that prefixes each line with Plop
        class MyStreamBuf: public std::stringbuf
        {
            std::ostream&   output;
            public:
                MyStreamBuf(std::ostream& str)
                    :output(str)
                {}
                ~MyStreamBuf() {
                    if (pbase() != pptr()) {
                        putOutput();
                    }
                }
       
            // When we sync the stream with the output. 
            // 1) Output Plop then the buffer
            // 2) Reset the buffer
            // 3) flush the actual output stream we are using.
            virtual int sync() {
                putOutput();
                return 0;
            }
            void putOutput() {
                // Called by destructor.
                // destructor can not call virtual methods.
                output << "[blah]" << str();
                str("");
                output.flush();
            }
        };
    
        // My Stream just uses a version of my special buffer
        MyStreamBuf buffer;
        public:
            MyStream(std::ostream& str)
                :std::ostream(&buffer)
                ,buffer(str)
            {
            }
    };
    
    
    int main()
    {
        MyStream myStream(std::cout);
        myStream << 1 << 2 << 3 << std::endl << 5 << 6 << std::endl << 7 << 8 << std::endl;
    }
    
    > ./a.out
    [blah]123 
    [blah]56 
    [blah]78
    >
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a class that supports __getitem__ , but does not allow
I want to define a class method that has access to a local variable.
I want to define a generic class in c++ that allow to execute my
I want to define a base class that defines a main method that instantiates
With an abstract class I want to define a method that returns "this" for
Here's the scenario: I want define a base class that allocate some buffer once
I want to define a class, ClassA , that can be passed as a
I want to define a Python class that accepts different variables as input. However
In PHP, if you define a class, and then instantiate an object of that
I want to define a new class that inherit the build in str type,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.