How to overload cout behaviour in c++

霸气de小男生 提交于 2019-12-20 03:11:53

问题


I would like to make cout will always print additional string(above and under) whenever I call it. It is actually weird to me cause I use in Java and C# primaly . EDIT: Maybe we can define other value for "y" string or something like that?

Example

code:

#include <iostream>
int main(){std::cout<<"y\n";}

result(printed):

x
y
z

I would not want to change int main() method just maybe overriding the << for the string type?

OR MAYBE make cout invoke additional method?


回答1:


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.



来源:https://stackoverflow.com/questions/14649920/how-to-overload-cout-behaviour-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!