Given:
typedef struct { char val[SOME_FIXED_SIZE]; } AString;
typedef struct { unsigned char val[SOME_FIXED_SIZE]; } BString;
I want to add ostream operator <<
available for AString
and BString
.
std::ostream & operator<<(std::ostream &out, const AString &str)
{
out.write(str.val, SOME_FIXED_SIZE);
return out;
}
If I do the same for BString
, the compiler complains about invalid conversion from 'const unsigned char*' to 'const char*'
. The ostream.write
does not have const unsigned char*
as argument.
It seems <<
itself accepts the const unsigned char
, so I try something like this
std::ostream & operator<<(std::ostream &out, const BString &str)
{
for (int i=0; i<SOME_FIXED_SIZE; i++)
{
out<<str.val[i];
}
return out;
}
Can someone tell me if this is right/good practice or there are some better ways? welcome any comments!