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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:44:21+00:00 2026-06-11T10:44:21+00:00

I am trying to make a Console class. I would like to wrap cin

  • 0

I am trying to make a Console class. I would like to wrap cin and cout in the class and overload the << and >> operators. So I could use the class as such:

// Output
Console << "Call cout from Console" << endl;

// Input
string str;
Console >> str; // Call cin

My best guess was:

class Console {
//...
public:
    ostream& operator<< (ostream& os)
    {
        cout << os;
        return &cout;
    }
    //...
};

But I know that’s wrong, how could I overload the operators to use the Console class as both cin and cout?

  • 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-06-11T10:44:22+00:00Added an answer on June 11, 2026 at 10:44 am

    I got a working code that can handle the stream manipulators. You can see my code in action in this Ideone page

    Here is the code:

    #include <iostream>
    
    typedef std::ostream& (*manip) (std::ostream&);
    
    class console {
    
    };
    
    template <class T> console& operator<< (console& con, const T& x) { std::cout << x; return con; }
    template <class T> console& operator>>(console& con,  T& x) { std::cin >>x; return con; }
    console& operator<< (console& con, manip manipulator){ std::cout<<manipulator; return con;}
    
    int main() {
        console c;
        int a,b;
        c>>a>>b;
        c << "hello world"<<std::endl<<std::hex<<(a+b)<<std::endl;
    }
    

    Thanks @MooingDuck for the discussion that led me to a working answer and @111111 for the start point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make use of java.io.Console . I am trying to do
I am trying to make a custom cout class that outputs text to console
I'm trying to make a console app that would monitor some process and restart
I'm trying to make my console application into a winforms application. The console version
So I'm trying to make a console app that imposes a time limit to
I'm trying to make a little console application that is able to deal with
I'm trying to make a cross platform console progress indicator in Java. Therefore I
I'm trying to make a little game for the console, so I need to
Possible Duplicate: C# full screen console? I am trying to make one of my
I'm trying to create a little Ruby hack to make something like the reverse

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.