c++streamoperator-overloading

operator<< overload for class with multiple layers


I am trying to get to the point where essentially MyClass(A) << "hi " << 17 << std::endl; compiles and executes MyClass::finish() method on the accumulated stream. So far, I'm still stuck on the "compiles" part. Here's what I have so far

#include <iostream>
#include <sstream>
#include <string>

enum foo
{
  A,
  B,
  C
};

class MyClass
{

  public:

    MyClass(foo v) : v_(v), oss_()
    {
    }

    MyClass& operator<<(std::ostream &o)
    {
      oss_ << o;
      return *this;
    }

  private:
    foo v_;
    std::ostringstream oss_;
};

int main()
{

  MyClass(A) << "hi " << 17 << std::endl;
  return 0;
}

Am I going down the wrong path?


Solution

  • Your operator<< is implemented wrong. It should not accept another ostream as input. Instead, it needs to accept all of the types that you want to stream in to your class and forward to your internal ostringstream. You can use a template to simplify that, eg:

    template<class T>
    MyClass& operator<<(const T &param)
    {
      oss_ << param;
      return *this;
    }
    
    MyClass& operator<<(std::ostream& (*func)(std::ostream&))
    {
      (*func)(oss_);
      return *this;
    }
    

    The 1st overload handles data types, and the 2nd overload handles stream manipulators like std::endl, which are implemented as functions.

    And then, you can call finish() inside your ~MyClass destructor.

    Live Demo