Every time I send a char to the cout object, it displays in ASCII characters unless I cast it to an int.
Q: Is there a way to display the numerical value of a char without an explicit cast?
I read somewhere that doing too many casts in your code could lead to a loss of integrity (of your program). I am guessing that chars display in ASCII for a particular reason, but I’m not sure why.
I am essentially creating a game. I am using small numbers (unsigned chars) that I plan to display to the console. I may be paranoid, but I get this uneasy feeling whenever I spam static_cast<int> everywhere in my code.
There is nothing wrong with type-casting, though, especially if you use
static_castto do it. That is what you should be using. It allows the compiler to validate the type-cast and make sure it is safe.To change the behavior of the
<<operator, you would have to override the default<<operator forcharvalues, eg:You could create a custom type that takes a
charas input and then implement the<<operator for that type, eg:You could create a function that does something similar, then you don’t have to override the
<<operator, eg: