How can I really create new types?

E

Ernesto

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
 
S

Siemel Naran

Ernesto said:
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?

You have to create a new class for each one.

You could create a template class to represent an unsigned number, then
instantiate a version of it for UCHAR, and another for BYTE. There might be
other better workaround solutions too.
 
H

Howard

Ernesto said:
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're 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
 
D

Denis Remezov

Ernesto said:
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


You can try something like this:

namespace TypeIdVal {
enum type {
uchar,
byte
//...
};
}

//see comments below
template <typename T, TypeIdVal::type id>
struct Value {
T value;

Value() : value(T()) {}
explicit Value(T const& v) : value(v) {}
Value& operator =(T const& v) {
value = v;
return *this;
}
};

template <typename T0, TypeIdVal::type id0,
typename T1, TypeIdVal::type id1>
bool operator ==(Value<T0, id0> const& lhs,
Value<T1, id1> const& rhs) {
return lhs.value == rhs.value;
}


typedef Value<unsigned char, TypeIdVal::uchar> uchar_t; //UCHAR;
typedef Value<unsigned char, TypeIdVal::byte> byte_t; //BYTE;

int main() {
//usage e.g.
uchar_t c0('f');
byte_t c1;
//...
c1 = 'g';
c0 == c1;
// cout<<c0.value<<endl;
}

Now uchar_t and byte_t are distinct types, though their semantics
is limited compared to the built-in types. The above is really only
a sketch, it needs to be refined. For example, in order to be able
to say c0=c1 (if you want to) you would need a template of operator =
rather than the one above. There are likely other things that I've
missed. The two things that I would, however, leave out are implicit
copy ctor and conversion operators.

Denis
 
D

Denis Remezov

Siemel said:
Good. But you don't need to define operator=.

Did you mean operator =(Value const&)? Then I write it off as an
optical illusion :). (Note the type of the parameter - it's T&).

Denis
 
D

Denis Remezov

Denis said:
[...]
a sketch, it needs to be refined. For example, in order to be able
to say c0=c1 (if you want to) you would need a template of operator =
rather than the one above.
[...]

You would need
template <typename T1, TypeIdVal::type id1>
Value& operator =(Value<T1, id1> const& rhs) {
value = rhs.value;
return *this;
}

/in addition/ to operator =(T const& v);

Denis
 
S

Siemel Naran

Denis Remezov said:
//see comments below
template <typename T, TypeIdVal::type id>
struct Value {
T value;

Value() : value(T()) {}
explicit Value(T const& v) : value(v) {}
Value& operator =(T const& v) {
value = v;
return *this;
}
};

Good. But you don't need to define operator=.
 
J

Jorge Rivera

Ernesto said:
Hi everybody:

I have the following typedefs:

typedef unsigned char UCHAR;
typedef unsigned char BYTE;

This is a bad idea. You are trying to differentiate the same type here.
UCHAR == BYTE == unsigned char.

Hence, defining

void foo(UCHAR)
{
}
void foo(BYTE)
{
}

should give you an error of function redefinition.
I am implementing a class String with the following operators overloaded:

String& operator+ (const UCHAR& myChar);
String& operator+ (const BYTE& myByte);

And your compiler doesn't whine about this?
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?

Not with your definition of UCHAR and BYTE. You must declare a struct...

struct UCHAR
{
UCHAR(unsigned char aChar)
: char_(aChar)
{}
unsigned char char_;
};

struct BYTE
{
BYTE(unsigned char aByte)
: byte_(aByte)
{}
unsigned char byte_;
}

Now, you cand define the appropriate operator+ implementations,
remembering that the data you desire is in byte_ and char_ members.

Jorge L.
 
S

Siemel Naran

Denis Remezov said:
Siemel Naran wrote:

Did you mean operator =(Value const&)? Then I write it off as an
optical illusion :). (Note the type of the parameter - it's T&).

Right, my mistake. But you're right in your other most, we need a using
base::eek:perator= declaration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top