> Programming Languages > C++
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on C++



C++ - "How can I really create new types?" in Programming Languages


Old 06-18-2004   #1
..nes..
 
Default How can I really create new types?

Hi everybody:

I have the following typedefs:

typedef unsigned char UCHAR;
typedef unsigned char BYTE;

I am implementing a cl*** 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
 
Old 06-18-2004   #2
..em.. ..r..
 
Default Re: How can I really create new types?

"Ernesto" <ebascon@hotmail.com> wrote in message

> typedef unsigned char UCHAR;
> typedef unsigned char BYTE;
>
> I am implementing a cl*** 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 cl*** for each one.

You could create a template cl*** 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.


 
Old 06-18-2004   #3
..wa..
 
Default Re: How can I really create new types?


"Ernesto" <ebascon@hotmail.com> wrote in message
news:f7c444ea.0406180801.13893e56@posting.google.c om...
> Hi everybody:
>
> I have the following typedefs:
>
> typedef unsigned char UCHAR;
> typedef unsigned char BYTE;
>
> I am implementing a cl*** 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 cl***es
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 p***ed 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 cl*** instead. That cl***
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




 
Old 06-19-2004   #4
..n.. ..mez..
 
Default Re: How can I really create new types?

Ernesto wrote:
>
> Hi everybody:
>
> I have the following typedefs:
>
> typedef unsigned char UCHAR;
> typedef unsigned char BYTE;
>
> I am implementing a cl*** 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
 
Old 06-19-2004   #5
..n.. ..mez..
 
Default Re: How can I really create new types?

Siemel Naran wrote:
>
> "Denis Remezov" <firstname_surname@yahoo.removethis.ca> wrote in message
>
> > //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=.


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
 
Old 06-19-2004   #6
..n.. ..mez..
 
Default Re: How can I really create new types?

Denis Remezov wrote:
>

[...]
> 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
 
Old 06-19-2004   #7
..em.. ..r..
 
Default Re: How can I really create new types?

"Denis Remezov" <firstname_surname@yahoo.removethis.ca> wrote in message

> //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=.


 
Old 06-19-2004   #8
..r.. ..ve..
 
Default Re: How can I really create new types?

Ernesto wrote:
> 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 cl*** 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.
 
Old 06-19-2004   #9
..em.. ..r..
 
Default Re: How can I really create new types?

"Denis Remezov" <firstname_surname@yahoo.removethis.ca> wrote in message
> Siemel Naran wrote:


> > > 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=.

>
> 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:perator= declaration.


 
Old 06-20-2004   #10
..em.. ..r..
 
Default Re: How can I really create new types?

"Siemel Naran" <SiemelNaran@REMOVE.att.net> wrote in message
news:rj_Ac.97560
> "Denis Remezov" <firstname_surname@yahoo.removethis.ca> wrote in message


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


Sorry again, there's no using declaration as there's no base cl***. We need
to define two operator=.


 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0