Handle displaying uint8 as int automatically for stream

I have a class with a member of type uint8, and when I try to output it to ostream, it displays it as a char. I would prefer this int representation, so I need static_cast (myStruct.member) to be a little cumbersome and potentially error-prone each time. Any ideas?

+3
source share
2 answers

Add operator<<to your class and define it. It seems to me that you break encapsulation.

+1
source
class X {
  uint8 a;
  int get_int () const { return static_cast<int>(a); }
};

Our wrapper method that encapsulates casting inside. Using:

cout << obj.get_int();
0
source

All Articles