I’d like to do this:
MyClass mc = MyClass('Some string' << anotherString);
Thanks for your answers, I have decided to re-write this question based on what you’ve told me, as it’s gotten a little messy. Eventually, I read C++ format macro / inline ostringstream, and decided to use a macro, as it’s not really possible to do this using a constructor. Some answers my no longer be relevant.
Now, what I can actually, do is:
MY_CLASS('Some string' << anotherString << ' more string!');
Using this macro:
#include <sstream> #define MY_CLASS(stream) \ MyClass( ( dynamic_cast<std::ostringstream &> ( \ std::ostringstream() . seekp( 0, std::ios_base::cur ) << stream ) \ ) . str() )
Where the MyClass constructor takes a string:
MyClass::MyClass(string s) { /* ... */ }
I think you should look at this question for some hints as to what will be required to get the behavior you want.
This sort of thing seems to a bit difficult.