"Ernesto" <eb*****@hotmai l.com> wrote in message
news:f7******** *************** ***@posting.goo gle.com...
Hi everybody:
I have the following typedefs:
typedef unsigned char UCHAR;
typedef unsigned char BYTE;
I am implementing a class String with the following operators overloaded:
String& operator+ (const UCHAR& myChar);
String& operator+ (const BYTE& myByte);
I want
String("HELLO") + (const UCHAR&) 'a' to return "HELLOa"
and
String("HELLO") + (const BYTE&) 12 to return "HELLO12"
but my compiler says: Redefinition of operator+ (const unsigned char&)
Is there a way to really create new types or workaround with my issue?
Thanks in advance
Ernesto
I'm not sure what you mean about creating new types. You can define classes
as you see fit, but using a typedef isn't really creating a new type, but
rmore like creating a shorthand notation for a (generally) more complex type
declaration, such as declaring BYTE as unsigned char.
You don't tell us how BYTE and UCHAR are defined, but from the error message
I'd guess that they're both actually defined as unsigned char. If that's
the case, then there's no difference between the two operators, so you get
an error that you've redefined the operator.
I don't see any way to do what you're asking, and not just because those two
types are identical, but also because there's no intrinsic difference
between 'a' and 12 (aside from the actual value)...they'r e both integer
types (unsigned char). Now if you passed 12345 instead of 12, that would
not be an unsigned char, but rather (I think) an unsigned int.
Perhaps you could declare an operator that took an unsigned int instead, but
then whenever you wanted to use the unsigned int version on a value that
"could" be an unsigned char, you'd have to cast the operand to unsigned int
to make sure the compiler called the right version. And your operator would
have to handle larger values, obviously. But then, what about long integers
(those that *might* fall outside the range of int)? And what about *signed*
integers? It's starting to look a little complex, isn't it?
You might want to look at using the stringstream class instead. That class
has the facilities to let you stream numbers into the string in various
formats, like decimal or hex, and is not limited to the range of an unsigned
char.
-Howard