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(); }