Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

1 solution

Please implement << operator function for byte.

C++
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;
}


Please refer following link to know more details about operator overloading.
http://www.cprogramming.com/tutorial/operator_overloading.html[^]
 
Share this answer
 
v3
Comments
unscathed18 31-May-13 6:45am    
OK, this works well, but by the simple type wrapping I am just hiding away the conversions in between. Isn't there anyway to show a "char" as a number instead of a ASCII display? I guess it should depend on the OS console. The solution you provided above needs to make a prior conversion whether to a non-char type or a string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900