How to override cout in C++?

前端 未结 9 1402
礼貌的吻别
礼貌的吻别 2020-12-07 02:47

I have a requirement, I need to use printf and cout to display the data into console and file as well. For printf I have

相关标签:
9条回答
  • 2020-12-07 03:46

    There's already a Boost class for this: tee

    0 讨论(0)
  • 2020-12-07 03:47

    cout is normally implemented as an object instance so you can't override it in the way that you would overload / override a function or a class.

    Your best bet is not to fight that - yes you could build a my_cout and #define cout my_cout but that would make your code obtuse.

    For readability I'd leave cout as it is. It's a standard and everyone knows what it can and can't do.

    0 讨论(0)
  • 2020-12-07 03:50

    Overriding the behaviour of std::cout is a really bad idea as other developers will have a hard time understanding that the use of std::cout doesn't behave as usual.

    Make your intention clear with a simple class

    #include <fstream>
    #include <iostream>
    
    class DualStream
    {
       std::ofstream file_stream;
       bool valid_state;
       public:
          DualStream(const char* filename) // the ofstream needs a path
          :
             file_stream(filename),  // open the file stream
             valid_state(file_stream) // set the state of the DualStream according to the state of the ofstream
          {
          }
          explicit operator bool() const
          {
             return valid_state;
          }
          template <typename T>
          DualStream& operator<<(T&& t) // provide a generic operator<<
          {
             if ( !valid_state ) // if it previously was in a bad state, don't try anything
             {
                return *this;
             }
             if ( !(std::cout << t) ) // to console!
             {
                valid_state = false;
                return *this;
             }
             if ( !(file_stream << t) ) // to file!
             {
                valid_state = false;
                return *this;
             }
             return *this;
          }
    };
    // let's test it:
    int main()
    {
       DualStream ds("testfile");
       if ( (ds << 1 << "\n" << 2 << "\n") )
       {
          std::cerr << "all went fine\n";
       }
       else
       {
          std::cerr << "bad bad stream\n";
       }
    }
    

    This provides a clean interface and outputs the same for both the console and the file. You may want to add a flush method or open the file in append mode.

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