我怎样才能真正创建新类型? [英] How can I really create new types?

查看:58
本文介绍了我怎样才能真正创建新类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:


我有以下typedef:


typedef unsigned char UCHAR;

typedef unsigned char BYTE;


我正在实现一个类String,以下运算符重载:


String& operator +(const UCHAR& myChar);

String& operator +(const BYTE& myByte);


我想要


String(" HELLO")+(const UCHAR&)''a ''返回'HELLOa"





String(" HELLO")+(const BYTE&)12返回" ; HELLO12"

但我的编译器说:重新定义operator +(const unsigned char&)

有没有办法真正创建新类型或解决我的问题?


提前致谢


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

推荐答案

" Ernesto" ; < EB ***** @ hotmail.com>在消息中写道
"Ernesto" <eb*****@hotmail.com> wrote in message
typedef unsigned char UCHAR;
typedef unsigned char BYTE;

我正在实现一个类String,并重载了以下运算符: br />
String& operator +(const UCHAR& myChar);
String& operator +(const BYTE& myByte);

我想要

字符串(HELLO)+(const UCHAR&)''a''返回'HELLOa"



字符串(HELLO)+(const BYTE&)12返回HELLO12

但我的编译器说:重新定义operator +(const unsigned char&)

有没有办法真正创建新类型或解决我的问题?
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?




你有为每个人创建一个新类。


你可以创建一个模板类来表示一个无符号数,然后

为UCHAR实例化它的一个版本,另一个是BYTE。可能还有

其他更好的解决方案。



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.




" Ernesto" < EB ***** @ hotmail.com>在消息中写道

news:f7 ************************** @ posting.google.c om ...

"Ernesto" <eb*****@hotmail.com> wrote in message
news:f7**************************@posting.google.c om...
大家好:

我有以下typedef:

typedef unsigned char UCHAR;
typedef unsigned char BYTE;

我正在实现一个类String,并重载了以下运算符:

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

我想要

字符串(HELLO)+(const UCHAR&)''a''返回'HELLOa"



字符串(HELLO)+(const BYTE&)12返回HELLO12

但我的编译器说:重新定义operator +(const unsigned char&)

有没有办法真正创建新类型或解决我的问题?

提前致谢

> 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




我不确定你对创建新类型的意思。您可以根据需要定义类

,但使用typedef并不是真正创建一个新类型,但

更像是为a创建一个简写符号(通常)更复杂的类型

声明,例如将BYTE声明为unsigned char。


你不能告诉我们BYTE和UCHAR是如何定义的,而是来自错误信息

我猜他们实际上都被定义为unsigned char。如果那个'是
的情况,那么两个运营商之间没有区别,所以你得到了

错误,你已经重新定义了运营商。


我没有看到任何方法可以做你要求的事情,而不仅仅因为那两个类型相同,而且因为'a'和12之间没有内在差异

(除了实际值)......它们都是整数

类型(无符号)字符)。现在如果你通过了12345而不是12,那就不会是一个unsigned char,而是(我认为)一个unsigned int。


也许你可以声明一个带有unsigned int的运算符,但是当你想在一个值上使用unsigned int版本时,



" can"如果是unsigned char,你必须将操作数转换为unsigned int

以确保编译器调用正确的版本。而且你的运营商显然需要处理更大的价值。但是,那么长整数呢?
(*可能*超出int范围的那些)?那么*签名*

整数呢?它开始看起来有点复杂,不是吗?


你可能想看一下使用stringstream类。那个类

有让你以各种

格式(如十进制或十六进制)将数字串流到字符串中的功能,并且不限于无符号的范围/>
char。


-Howard




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



Ernesto写道:
Ernesto wrote:

大家好:

我有以下typedef:

typedef unsigned char UCHAR;
typedef unsigned char BYTE;

我正在实现一个类String,并重载了以下运算符:

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

我想要

字符串(HELLO)+(const UCHAR&)''a''返回'HELLOa"



字符串(HELLO)+(const BYTE&)12返回HELLO12

但我的编译器说:重新定义operator +(const unsigned char&)

有没有办法真正创建新类型或解决我的问题?

提前致谢

> 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



您可以尝试这样的事情:


命名空间TypeIdVal {

枚举类型{

uchar,

byte

// ...

};

}


//见下面的评论

模板< typename T,TypeIdVal :: type id>

struct Value {

T值;


Value():value(T()){}

显式值(T const& v):value( v){}

价值& operator =(T const& v){

value = v;

return * this;

}

};


模板< 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值< unsigned char,TypeIdVal :: uchar> uchar_t; // UCHAR;

typedef值< unsigned char,TypeIdVal :: byte> byte_t; // BYTE;


int main(){

//用法例如

uchar_t c0(''f'') ;

byte_t c1;

// ...

c1 =''g'';

c0 == c1;

// cout<< c0.value<< endl;

}


现在uchar_t和byte_t是不同的类型,尽管它们的语义与内置类型相比是有限的。以上实际上只是一个草图,需要进一步细化。例如,为了能够说b0 = c1(如果你想),你需要一个operator =

的模板而不是上面的模板。可能还有其他的事情我错过了b $ b。然而,我遗漏的两件事是隐含的

复制ctor和转换操作符。


Denis


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


这篇关于我怎样才能真正创建新类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆