I have a class Bitset
that stores a vector
of chars, and I want to be able, whenever I use cout << char
, to cast that char
into a short int ONLY if it is part of that class.
Code:
template<long long X> class Bitset { public: std::vector<unsigned char> bit = std::vector<unsigned char> ((X+7)/8); public: /* constructors */ friend std::ostream &operator<< (std::ostream &output, const char x); }; std::ostream &operator<< (std::ostream &output, const char x) { output<<(short)(x); return output; }
The idea is that if I write:
Bitset a; /* code */ cout << a.bit[x]; cout << 'a';
I want to cast a.bit[x]
into a short, but not 'a'
as well.
cout << a.bit[x];
to always print a number instead of an ASCII character? This will need something other thanchar
, but you could do it with a simple struct that wraps a char. You could also usestd::byte
to more clearly document that this is not a character – alter igel