0

I have the class shown below. I used to be able to create a StrStrmBuf object and pass it to std::cout like this:

#using namespace std;
StrStrmBuf ssb("Hello");
cout << ssb << endl << endl;

but with C++11 I have to do this.

StrStrmBuf ssb("Hello");
cout << ssb.str() << endl << endl;

I think that I was able to use the first form because of

operator const char*() { return str().c_str(); }

but it doesn't see to work with C++ 11. I have been trying to overload the << operator but without success. (See the non-member << overload shown at the end of the listing.) Can anyone suggest how I should implement the << overload for this class? I'd like to be able to simply say cout << ssb; Thank you.

#define SSB_OPEN_MODE std::ios_base::out|std::ios_base::in|std::ios_base::ate
class StrStrmBuf : public std::stringstream
{
public:
    explicit StrStrmBuf() : std::stringstream(SSB_OPEN_MODE)    {}
    explicit StrStrmBuf(const std::string& str) :
    std::stringstream(str, SSB_OPEN_MODE)           {}
    explicit StrStrmBuf(const char* pStr) :
    std::stringstream(pStr, SSB_OPEN_MODE)          {}
    template <typename T>
    StrStrmBuf(const std::string& str, T const param) :
        std::stringstream(str, SSB_OPEN_MODE)  { *this << param; }
    StrStrmBuf(StrStrmBuf& rhs)                { str(rhs.str()); }
    StrStrmBuf& putch(char ch, size_t nCount = 1);
    StrStrmBuf& putstr(const std::string& str);

    StrStrmBuf& width(int nCol)
        { std::ios_base::width(nCol); return *this; }
    StrStrmBuf& clear()         { str(""); return *this;  }
    size_t length() const           { return str().length();  }
    bool empty() const          { return str().length() == 0; }
    StrStrmBuf& operator=(const std::string& s) { str(s); return *this; }
    StrStrmBuf& operator=(const char* s)        { str(s); return *this; }
    StrStrmBuf& operator=(StrStrmBuf& rhs);
    operator const char*()                      { return str().c_str(); }
    operator const std::string()                { return str();         }

    StrStrmBuf& ltrim();
    StrStrmBuf& rtrim();
    StrStrmBuf& trim();

    StrStrmBuf& lpad(const std::string& s, size_t nSize, char ch = ' ');
    StrStrmBuf& rpad(const std::string& s, size_t nSize, char ch = ' ');
    StrStrmBuf& center(const std::string& s, size_t nSize, char ch = ' ');
    };

    const char* operator<<(std::ostream& out, StrStrmBuf& rhs) 
    { return      rhs.str().c_str(); }
john
  • 1
  • I looked at many similar posts including the one you say answers this question, but I don't see the solution to my problem. This isn't a class that uses a std::stringstream, is is a class that is a std::stringstream. I would appreciate a response. – john Jun 25 '18 at 00:25
  • Operator << should send the string to the ostream, then return it – Dominic Price Jun 25 '18 at 01:07
  • For example see https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx – Dominic Price Jun 25 '18 at 01:08
  • I suggest: https://www.geeksforgeeks.org/overloading-stream-insertion-operators-c/ – 2785528 Jun 25 '18 at 01:21

0 Answers0