问题描述
我想让cout每次调用时都将始终打印其他字符串(上下).对我来说很奇怪,因为我主要在Java和C#中使用.
I would like to make cout will always print additional string (above and under) whenever I call it. It is weird to me because I use in Java and C# primarily.
也许我们可以为"y"定义其他值.字符串或类似的东西?
Maybe we can define other value for "y" string or something like that?
代码:
#include <iostream>
int main(){std::cout<<"y\n";}
结果(打印):
x
y
z
我不想更改 int main()
方法,而只是覆盖字符串类型的<<
?
I would not want to change int main()
method just maybe overriding the <<
for the string type?
还是让cout调用其他方法?
Or maybe make cout invoke additional method?
推荐答案
我认为您不能使用cout做到这一点.相反,您可以做的是创建一个简单的I/O包装器,进行额外的打印,然后调用cout进行实际工作.
I don't think you can do this with cout. What you can do instead is create a simple I/O wrapper that does your extra printing and then calls into cout to do the real work.
这篇关于如何在C ++中重载cout行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!