【发布时间】:2009-03-19 22:35:07
【问题描述】:
我想这样做:
MyClass mc = MyClass("Some string" << anotherString);
感谢您的回答,我决定根据您告诉我的内容重新编写这个问题,因为它有点乱。最终,我阅读了C++ format macro / inline 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) { /* ... */ }
【问题讨论】:
标签: c++ string stringstream