我想这样做:
MyClass mc = MyClass("Some string" << anotherString);
感谢您的回答,我决定根据您告诉我的内容重新编写这个问题,因为它变得有点混乱。最终,我阅读了C++ 格式的宏/内联 ostringstream,并决定使用宏,因为使用构造函数实际上不可能做到这一点。有些答案我不再相关。
现在,我实际上可以做的是:
MY_CLASS("Some string" << anotherString << " more string!");
使用这个宏:
#include <sstream>
#define MY_CLASS(stream) \
MyClass( ( dynamic_cast<std::ostringstream &> ( \
std::ostringstream() . seekp( 0, std::ios_base::cur ) << stream ) \
) . str() )
MyClass 构造函数采用字符串的位置:
MyClass::MyClass(string s) { /* ... */ }