帮助typedef [英] Help with typedef

查看:72
本文介绍了帮助typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真正想做的是定义我自己的类型,它并不是真的

无关紧要。

无论如何,我遇到了一些问题


1)

typedef unsigned short U16;

U16 test = 0xffffffff; //应该有一个限制,最大值

你应该能够asign应该是0xffff //

std :: cout<<测试; // result = 0xffff //


如果没有解决方案,这不是问题,但是我不确定

解决方案所在的位置。


2)

typedef unsigned:8 U8; //这不起作用,我希望如此。可以吗?b
可以吗? //


也许typedef不是可行的方法,但有什么选择?


提前致谢。


Zacariaz

解决方案

fe ********** @ hotmail.com 写道:

我真正想做的是,定义我自己的类型,它不是真的
为什么。
无论如何,我遇到了一些问题

1)
typedef unsigned short U16;


没有定义新类型,它将现有类型别名。

U16 test = 0xffffffff; //应该有一个限制,最大值
你应该能够asign应该是0xffff //
std :: cout<<测试; // result = 0xffff //


那么,您想要编译时诊断吗?运行中的东西

时间?一个编辑器,阻止你键入更多的''f'?


它还依赖于平台/编译器 - unsigned short不会

必须是16位。

如果没有解决方案,这不是问题,但我不确定解决方案所在的位置。


好​​吧,我不确定是什么问题。

2)
typedef unsigned:8 U8; //这不起作用,我希望如此。可以吗? //


没有那种语法。

也许typedef不是要走的路,但有什么选择?




您可以使用自己的类型(结构或类),但我不确定您试图解决的问题是什么




我想*你想要一个给定大小的整数类型(以位为单位)。

在C ++中没有标准的方法。


看看提升:
http://www.boost.org/libs/integer/integer.htm

具体来说,标题:
http://www.boost.org/boost/cstdint.hpp


并且还在C99标题处,stdint.h,也可以找到

< cstdint>与你的图书馆。 (或者可能不是)。


这些都不是C ++标准,但将来很可能接近




Ben Pope

-

我不仅仅是一个数字。对很多人来说,我被称为字符串...


fe ********** @ hotmail.com 写道:

我真正想做的是定义我自己的类型,它不是真的
无论如何。
无论如何,我遇到了一些问题

1)
typedef unsigned short U16;
U16 test = 0xffffffff; //应该有一个限制,最大值
你应该能够asign应该是0xffff //
std :: cout<<测试; // result = 0xffff //

没有解决方案这不是问题,但我不确定解决方案所在的位置。


没有。截断是由编译器在

初始化时完成的。你明确允许初始化一个unsigned short

,其表达式产生unsigned int或unsigned long。

2)
typedef unsigned:8 U8 ; //这不起作用,我希望如此。可以吗? //

也许typedef不是要走的路,但是有什么选择?




如果你的系统有八位字节,你就是应该可以说


typedef unsigned char U8;


但是如果它没有,那就没办法了。一个最小的可寻址元素

您的计算机内存是一个字节(''char''),并且您不能声明一个对象小于任何大小的对象



V

-

请在邮寄回复时从我的地址删除资金




Ben Pope skrev:

fe ********** @ hotmail.com 写道:

我真正想做的是,定义我自己的类型,它不是真的
问题为什么。
无论如何,我遇到了一些问题

1)
typedef unsigned short U16;



这并没有定义一个新类型,它将现有类型别名。

U16 test = 0xffffffff; //应该有一个限制,最大值
你应该能够asign应该是0xffff //
std :: cout<<测试; // result = 0xffff //



那么,您希望编译时诊断?运行的东西
时间?一个阻止你输入更多''f'的编辑器?

它还依赖于平台/编译器 - 无符号短路不必是16位。

如果没有解决方案,这不是问题,但我不确定解决方案所在的位置。



嗯,我''我不确定是什么问题。

2)
typedef unsigned:8 U8; //这不起作用,我希望如此。可以吗? //



没有那种语法。

也许typedef不是要走的路,但是替代方案是什么?



您可以使用自己的类型(结构或类),但我不确定您要解决的问题是什么。

我*想*你想要给定大小的整数类型(以位为单位)。
在C ++中没有标准的方法。

看看boost:
http://www.boost.org/libs/integer/integer .htm
具体来说,标题:
http://www.boost.org/boost/cstdint.hpp

还有C99标题,stdint.h,也可能被发现为
< cstdint>与你的图书馆。 (或者可能不是)。

这些都不是C ++标准,但将来很可能会接近
。本教皇
-
我不仅仅是一个数字。对于很多人来说,我被称为字符串...




说它就像有形一样简单。


我需要四种类型:


unsigned char

unsigned short

unsigned long

unsigned long long


首先,我想重新定义他们的名字,因为他们占据了多少位b / b



unsigned char - U8

unsigned short - U16

unsigned long - U32

unsigned long long - U64


签名字符 - S8

签名短 - S16

签名长 - S32

签长多 - S64


或类似的东西。


其次,无符号字符我非常喜欢。

是的真的,你不必在一个字母中存储一个字母,但如果你需要再次提取数字,那么你需要再次提取这个数字。

std :: cout< ;< static_cast< int>(char_var);

是这样做的方法,如果我没弄错的话。


正如我所说,这是一个问题解决方案,但我不太喜欢

解决方案。


What i really wanna do, is defining my own types, it doesnt really
matter why.
Anyway, i have run into some problems

1)
typedef unsigned short U16;
U16 test = 0xffffffff; // There should be a limit on this, max value
you should be able to asign should be 0xffff //
std::cout << test; // result = 0xffff //

This is not a problem without at solution, however i am no certain of
where the solution lies.

2)
typedef unsigned:8 U8; // This doesnt work, i would like it to. Can it
be done? //

Maybe typedef isnt the way to go, but what is the alternatives?

Thanks in advance.

Zacariaz

解决方案

fe**********@hotmail.com wrote:

What i really wanna do, is defining my own types, it doesnt really
matter why.
Anyway, i have run into some problems

1)
typedef unsigned short U16;
That doesn''t define a new type, it aliases an existing type.
U16 test = 0xffffffff; // There should be a limit on this, max value
you should be able to asign should be 0xffff //
std::cout << test; // result = 0xffff //
So what, you would like a compile time diagnostic? Something at run
time? An editor that prevents you from typing more ''f''s?

It''s also platform / compiler dependant - an unsigned short does not
have to be 16bits.
This is not a problem without at solution, however i am no certain of
where the solution lies.
Well, I''m not sure what the problem is.
2)
typedef unsigned:8 U8; // This doesnt work, i would like it to. Can it
be done? //
Not with that syntax.
Maybe typedef isnt the way to go, but what is the alternatives?



You could use your own type (a struct or class), but I''m not sure what
problem you''re trying to solve.

I *think* you want integer types that are of a given size (in bits).
There is no standard way of doing it in C++.

Take a look at boost:
http://www.boost.org/libs/integer/integer.htm
specifically, the header:
http://www.boost.org/boost/cstdint.hpp

And also at the C99 header, "stdint.h", which may also be found as
<cstdint> with your library. (or may not).

Neither of these are C++ standard, but something approximating either
are very likely to be, in the future.

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


fe**********@hotmail.com wrote:

What i really wanna do, is defining my own types, it doesnt really
matter why.
Anyway, i have run into some problems

1)
typedef unsigned short U16;
U16 test = 0xffffffff; // There should be a limit on this, max value
you should be able to asign should be 0xffff //
std::cout << test; // result = 0xffff //

This is not a problem without at solution, however i am no certain of
where the solution lies.
There is none. The truncation is done by the compiler at the time of
initialisation. You''re explicitly allowed to initialise an unsigned short
with an expression that produces an unsigned int or unsigned long.
2)
typedef unsigned:8 U8; // This doesnt work, i would like it to. Can it
be done? //

Maybe typedef isnt the way to go, but what is the alternatives?



If your system has eight-bit bytes, you should be able to say

typedef unsigned char U8;

But if it doesn''t, there is no way. A minimal addressable element of
your computer memory is a byte (''char''), and you cannot declare an object
of any size smaller than that stand-alone.

V
--
Please remove capital As from my address when replying by mail



Ben Pope skrev:

fe**********@hotmail.com wrote:

What i really wanna do, is defining my own types, it doesnt really
matter why.
Anyway, i have run into some problems

1)
typedef unsigned short U16;



That doesn''t define a new type, it aliases an existing type.

U16 test = 0xffffffff; // There should be a limit on this, max value
you should be able to asign should be 0xffff //
std::cout << test; // result = 0xffff //



So what, you would like a compile time diagnostic? Something at run
time? An editor that prevents you from typing more ''f''s?

It''s also platform / compiler dependant - an unsigned short does not
have to be 16bits.

This is not a problem without at solution, however i am no certain of
where the solution lies.



Well, I''m not sure what the problem is.

2)
typedef unsigned:8 U8; // This doesnt work, i would like it to. Can it
be done? //



Not with that syntax.

Maybe typedef isnt the way to go, but what is the alternatives?



You could use your own type (a struct or class), but I''m not sure what
problem you''re trying to solve.

I *think* you want integer types that are of a given size (in bits).
There is no standard way of doing it in C++.

Take a look at boost:
http://www.boost.org/libs/integer/integer.htm
specifically, the header:
http://www.boost.org/boost/cstdint.hpp

And also at the C99 header, "stdint.h", which may also be found as
<cstdint> with your library. (or may not).

Neither of these are C++ standard, but something approximating either
are very likely to be, in the future.

Ben Pope
--
I''m not just a number. To many, I''m known as a string...



To say it as simple as posible.

I need four type:

unsigned char
unsigned short
unsigned long
unsigned long long

First of all i would like to redefine their names as to how many bit
they occupy and wether they are signed or not:

unsigned char - U8
unsigned short - U16
unsigned long - U32
unsigned long long - U64

signed char - S8
signed short - S16
signed long - S32
signed long long - S64

or something like it.

Second, the unsigned char i really enoying.
Yes its true, you dont have to store a letter in a char, but if you
store numbers, every time you need to pull out the number again.
std::cout << static_cast<int>(char_var);
is the way to do it, if im not mistaken.

As i said, it is a problem with a solution, but i dont much like the
solution.


这篇关于帮助typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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