处理“字节”时改变COUT行为我自己定义的类型为typedef char。 [英] Changing COUT behaviour when dealing with "byte" type defined by myself as typedef char.
本文介绍了处理“字节”时改变COUT行为我自己定义的类型为typedef char。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在显示char时改变控制台中显示的行为?
我的意思是,我创建了一个名为byte的类型,它来自char但是我希望byte显示一个数字,而不是ASCII表中的符号。
建议?
谢谢。
Is there any way to alter the behavior shown in the console when displaying a char?
I mean, I created my type called byte, which comes from char but I want byte to show a number, not a symbol from the ASCII table.
Suggestions?
Thanks.
推荐答案
请实施<<字节的运算符函数。
Please implement << operator function for byte.
class Byte
{
friend ostream& operator<<(ostream& s, Byte b)
{
s<<( b.m_a ); // Here display whatever you want with your member. Prepare integer version display or other formatted string, and put to the output stream
return s;
}
};
// From main function
main()
{
byte b(20);
cout<<b;
}
请参考以下链接了解有关运营商超载的更多详情。
http://www.cprogramming.com/tutorial/operator_overloading.html [ ^ ]
这篇关于处理“字节”时改变COUT行为我自己定义的类型为typedef char。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文