类似 QDebug 的结构:通过“operator<<”确定输入结束
Qt 有一个很好的调试功能,
qDebug() << first_qobject << second_qobject;
它会生成一行带有对象的一些“标准字符串”的行,并且 - 这是重要的部分 - 打印 \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()
似乎能够做到这一点。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到解决方案,发现我的问题也是重复的:
How does QDebug() <<东西;自动添加换行符?
简而言之,这可以通过实现析构函数并创建临时
MyDebug
对象来完成,就像我在上面的代码和qDebug
中所做的那样是吗:Found the solution and found out that my question is also a duplicate:
How does QDebug() << stuff; add a newline automatically?
In short, this can be done by implementing the destructor and just create temporary
MyDebug
objects like I did it in the code above andqDebug
does it: