Let's say I have a char a;
variable in C++, and I want to make it an unsigned char
. Both datatype contains exactly one byte; the byte will stay the same when we do casting, so we are not really doing anything to the bytes.
However, I cannot change the type of variable a
. If I declare a new variable unsigned char b = (unsigned char) a;
, I am allocating memory for b
(and using lots of memory if I have lots of such variables to cast), and I am doing unnecessary copying. How can I change the type of a
without such copying?
Secondly, if I have std::vector<char> v;
and I wish to cast it to std::vector<unsigned char>
, what's the most efficient way to do so (without copying)? (Imagine a situation when you have char
but some function takes unsigned char
as its input.)