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 3787636
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:52:19+00:00 2026-05-19T11:52:19+00:00

I need a class that redirects one ostream to another ostream during the lifetime

  • 0

I need a class that redirects one ostream to another ostream during the lifetime of its object. After some tinkering I came up with this:

#include <iostream>
#include <fstream>


class ScopedRedirect
{
public:
    ScopedRedirect(std::ostream & inOriginal, std::ostream & inRedirect) :
        mOriginal(inOriginal),
        mRedirect(inRedirect)
    {
        mOriginal.rdbuf(mRedirect.rdbuf(mOriginal.rdbuf()));
    }

    ~ScopedRedirect()
    {
        mOriginal.rdbuf(mRedirect.rdbuf(mOriginal.rdbuf()));
    }    

private:
    ScopedRedirect(const ScopedRedirect&);
    ScopedRedirect& operator=(const ScopedRedirect&);

    std::ostream & mOriginal;
    std::ostream & mRedirect;
};


int main()
{
    std::cout << "Before redirect." << std::endl;
    std::ofstream filestream("redirected.txt");
    {
        ScopedRedirect redirect(std::cout, filestream);
        std::cout << "During redirect." << std::endl;
    }
    std::cout << "After redirect." << std::endl;

    return 0;
}

It seems to work fine. However, it’s weird that the following line is repeated in both the constructor and destructor:

mOriginal.rdbuf(mRedirect.rdbuf(mOriginal.rdbuf()));

I think it’s correct, but I would like to verify with the SO community. Can you find any errors or dangers in this code?

Edit

Make non-copyable.

  • 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-19T11:52:19+00:00Added an answer on May 19, 2026 at 11:52 am

    The reason those lines are the same is because what you’re doing is swapping the buffers. (That is, you “redirect” by swapping the original buffer with the redirect buffer; restoration is the swap back.)

    While this might give you the intended effect with respect to the output stream, it’s not correct because the redirect stream now outputs somewhere else. To redirect means to take one stream and make it output somewhere else; note this doesn’t effect that ‘somewhere else’.

    Your class is not a redirect; as is, it should really be named ScopedStreamSwap. For example, try this instead:

    #include <iostream>
    #include <fstream>
    
    class ScopedRedirect
    {
    public:
        ScopedRedirect(std::ostream & inOriginal, std::ostream & inRedirect) :
            mOriginal(inOriginal),
            mRedirect(inRedirect)
        {
            mOriginal.rdbuf(mRedirect.rdbuf(mOriginal.rdbuf()));
        }
    
        ~ScopedRedirect()
        {
            mOriginal.rdbuf(mRedirect.rdbuf(mOriginal.rdbuf()));
        }    
    
    private:
        ScopedRedirect(const ScopedRedirect&);
        ScopedRedirect& operator=(const ScopedRedirect&);
    
        std::ostream & mOriginal;
        std::ostream & mRedirect;
    };
    
    
    int main()
    {
        std::cout << "Before redirect." << std::endl;
        std::ofstream filestream("redirected.txt");
        {
            ScopedRedirect redirect(std::cout, filestream);
            std::cout << "During redirect." << std::endl;
    
            // oops:
            filestream << "also to the file, right?...nope" << std::endl;
            filestream << "ah, why am i on the screen?!" << std::endl;
        }
        std::cout << "After redirect." << std::endl;
    
        // in main, return 0 is implicit, if there is no return statement;
        // helpful to keep in mind in snippets and short things
    }
    

    What you want is this:

    #include <iostream>
    #include <fstream>
    
    class ScopedRedirect
    {
    public:
        ScopedRedirect(std::ostream & inOriginal, std::ostream & inRedirect) :
            mOriginal(inOriginal),
            mOldBuffer(inOriginal.rdbuf(inRedirect.rdbuf()))
        { }
    
        ~ScopedRedirect()
        {
            mOriginal.rdbuf(mOldBuffer);
        }    
    
    private:
        ScopedRedirect(const ScopedRedirect&);
        ScopedRedirect& operator=(const ScopedRedirect&);
    
        std::ostream & mOriginal;
        std::streambuf * mOldBuffer;
    };
    
    
    int main()
    {
        std::cout << "Before redirect." << std::endl;
        std::ofstream filestream("redirected.txt");
        {
            ScopedRedirect redirect(std::cout, filestream);
            std::cout << "During redirect." << std::endl;
    
            // yay:
            filestream << "also to the file, right?...yes" << std::endl;
            filestream << "i am not on the screen" << std::endl;
        }
        std::cout << "After redirect." << std::endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a Java Comparator class that compares Strings, however with one
I have a class that I need to binary serialize. The class contains one
I understand the need to test a class that has logic (for instance, one
I need to create at runtime instances of a class that uses generics, like
I have a class that defines a CallRate type. I need to add the
I'm busy writing a class that monitors the status of RAS connections. I need
Using C#, I need a class called User that has a username, password, active
I need your expertise once again. I have a java class that searches a
I have a not-so-small class under development (that it changes often) and I need
Often I find myself filling ASP.NET repeaters with items that need the CSS class

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.