Qt 有一个很好的调试功能,像这样调用
qDebug() << first_qobject << second_qobject;
它产生一条带有一些“标准到字符串”的对象的行,并且 - 这是重要的部分 - 打印 a\n
并在之后冲洗蒸汽second_object
。std::string to_string()
我想通过我所有的类都有一个我称之为方法的约定来重现这种行为:
struct myDebug{
template<typename T>
myDebug& operator<<(T t){
std::cout << t.to_string() << " "; // space-separated
return *this;
}
};
struct Point{
std::string to_string(){ return "42"; }
};
myDebug() << Point() << Point(); // should produce "42 42" plus a newline (which it doesn't)
我现在的问题是:有没有办法找出*this
第二次返回后返回的对象不再被调用?这样我就可以打印std::endl
? qDebug()
似乎能够做到这一点。