用C++实现类似JAVA类中的toString函数,javatostring
最近在开发时,发现C++的类中没有JAVA类中toString函数,打印对象的信息十分麻烦,就用宏自己实现了一个
主要代码如下
#define PRINT_CLASS_INFO_BEGIN(sstream, className) sstream << "ClassName:" << #className << ", Addr:" << this << "[";
#define PRINT_CLASS_PARA(sstream, para) sstream << #para << " = " << para;
#define PRINT_CLASS_INFO_END(sstream) sstream << "]" << std::endl;
class MyClass
{
public:
virtual string toString();
private:
int m_iPara1;
string m_strPara2;
}
string MyClass::toString
{
stringstream sstream;
PRINT_CLASS_INFO_BEGIN(sstream, MyClass)
PRINT_CLASS_PARA(sstream, m_iPara1)
PRINT_CLASS_PARA(sstream, m_strPara2)
PRINT_CLASS_INFO_END(sstream)
return sstream.str();
}
相关文章暂无相关文章