473,804 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 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
Jul 22 '05 #1
9 2188
"Ernesto" <eb*****@hotmai l.com> wrote in message
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.
Jul 22 '05 #2

"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


Jul 22 '05 #3
Ernesto wrote:

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::ucha r> 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
Jul 22 '05 #4
"Denis Remezov" <fi************ ***@yahoo.remov ethis.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=.
Jul 22 '05 #5
Siemel Naran wrote:

"Denis Remezov" <fi************ ***@yahoo.remov ethis.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
Jul 22 '05 #6
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
Jul 22 '05 #7
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 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.
Jul 22 '05 #8
"Denis Remezov" <fi************ ***@yahoo.remov ethis.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::operator= declaration.
Jul 22 '05 #9
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote in message
news:rj_Ac.9756 0
"Denis Remezov" <fi************ ***@yahoo.remov ethis.ca> wrote in message Right, my mistake. But you're right in your other most, we need a using
base::operator= declaration.


Sorry again, there's no using declaration as there's no base class. We need
to define two operator=.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2186
by: Martin Mrazek | last post by:
Hi, I have tables named like ag97a027, ag98a027, ... where 97 and 98 is from year 1997, 1998 and a027 is just specification of product type. I need carry out repeated operataions over these tables. To generate their names is easy, for example use declare @Yint int, @Y char(2), @tabName char(8) set @Yint = 1997 while @Yint < 2002
6
6026
by: Jordi Vilar | last post by:
Hi All, Is there a way to dynamic_cast a pointer to a type defined by a type_info*? something like: type_info *info_of_type_to_cast = typeid(type_to_cast); type_to_cast *casted = really_dynamic_cast<info_of_type_to_cast>(my_data_ptr);
26
1817
by: DeMarcus | last post by:
Let's say we're gonna make a system library and have a class with two prerequisites. * It will be used _a_lot_, and therefore need to be really efficient. * It can have two different implementations. (e.g. Unix/Windows) I feel stuck. The only solution I've seen so far is using the design pattern 'abstract factory' that gives me a pointer to a pure virtual interface (which can have whatever implementation). But that forces me to make a...
6
2371
by: billy | last post by:
I've got a set of subclasses that each derive from a common base class. What I'd like to do is create a global array of the class types (or, class names) that a manager class can walk through in its constructor and instantiate one of each of the class types in this global array. So, it's almost like the global array has to hold data types as opposed to data. Basically, I currently have to add the items manually 1 at a time.
8
1723
by: Peter Olcott | last post by:
Exactly why does C# and .NET require all the extra copying of data?
11
3444
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the difference between data type 'CHAR' and 'TEXT'? When do you use 'VAR' in your datatype word? e.g. VARCHAR ?
8
2342
by: Vincent RICHOMME | last post by:
Hi, first I would like to apologize about my question because usually I hate specific implementation but in this case I really would like an answer. Besides my example is an example of what's not to be done but I am aware of that. Don't loose time tell me that I should use standard implementation and consider my question as theoretical. So I am using Microsoft compiler and I have a class with lots of methods with get and set :
0
2201
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
5
4208
by: Tom van Stiphout | last post by:
I'm designing a new database from scratch. It has some columns that I currently have as datatype=int with check constraint of x>=1 and x <=32, or others with a range of 1-3, etc. It occurred to me I could create a UDDT to represent each. Benefit would be that I don't have to enter the check constraints all the time. Is there any consensus on whether or not this is a good idea? I am a bit concerned my front-end application (.NET) will not...
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.