C中的多态性(非常基本) [英] Polymorphism in C (very basic)

查看:52
本文介绍了C中的多态性(非常基本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,


我有一个包含32个值的数组。这使得根据单独的枚举提供的索引访问此数组中的
元素非常快。这个

数组的类型是unsigned long int。我有一个typedef:


typedef unsigned long int Uint32;

typedef float Float32;

Uint32 myArray [100] ;


在C ++中有铸造操作符,但是我在C中编写我的软件。

我想告诉编译器解释一个值作为一个浮点数(这个平台上的32位

降级,只需让编译器改变其对类型的解释)。


这很容易完成


Uint32 u32;

Float32 f32;


f32 = 3.1415;

u32 = *(Uint32 *)(& f32);


现在,如果您将u32打印到控制台,它将打印出类似于
$ b $的内容b垃圾(绝对不是数字3),但二进制表示仍然是

相同。


同样可以使用Union结构(但是以上是更好的我认为。


但为了便于阅读,我想将所有这些结合到

出现在一行不必先将数字放入变量

或注册,这样:


u32 = ConvertToFloat(3.14159);


我不想写一个函数 - 开销很高,我现在需要

把这个函数放在天秤座里需要链接的(这是多个二进制文件使用的
,有点像插件)建筑)。此外,

它依赖于编译器,如果这样的函数是内联的或不是

(C不提供内联,就我而言知道,如果可能的话,我仍然希望所有内容都在

头文件中。在标题

文件中有一个宏可以做到这一点很方便。


有谁知道怎么写这样的宏?


有没有可以用Microsoft Visual C ++汇编的东西?

我对英特尔组装一无所知。


我能想到的另一个替代方案是32位值的数组,它从我明确设置的类型派生出它的类型。

解决方案



2004年7月8日星期四,Jason Curl写道:


typedef unsigned long int Uint32;
typedef float Float32;
Uint32 myArray [100];

在C ++中有一些转换操作符,但是我正用C编写我的软件。


C也有类型铸造操作员;他们不是很多,而且他们并不是那么丑陋。 :)

我试图告诉编译器将值解释为float(此平台上的32位
实体),但将其存储到Uint32数组中,我将其存储之后
采取并转换回浮动。 (我不想进行类型升级或降级,只需让编译器更改其类型的解释)。
同样可以用联盟结构来完成(但上面的内容更好我认为)。


但便携性要差得多。就此而言,你为什么要使用* *
*整数*来存储* float *值?!只需使用一系列

花车!这不是因为它的惩罚!

但为了便于阅读,我想把所有这些结合起来,而不是先把数字放在一行上。变成一个变量
或注册,这样:

u32 = ConvertToFloat(3.14159);


*(Float32 *)u32 = 3.14159;


或上述明显的宏观化。

我不想写一个函数 - 开销很高,我现在需要把这个函数放在一个需要链接的库中(这是由多个二进制文件使用的,有点像插件架构)。此外,
它依赖于编译器,如果这样的函数是内联的或不是
(据我所知,C并不提供内联,我仍然希望所有内容都在一个
头文件,如果可能的话)。在标题
文件中有一个宏可以做到这一点很方便。


C99提供''inline''关键字;在C90,你可以写


的#ifndef CNVT2FLOAT

的#define CNVT2FLOAT

静态UINT32 Cnvt2Float(浮点32 F){回报*( UINT32 *)&安培; F; }

#endif


我能想到的另一个选择是32位值的数组,它从我明确设置的东西中派生出它的类型它来。




这句话对我来说没有意义。你真的*尝试

做什么,为什么?当然没有实际的理由在代码中使用''Uint32''

而不是''Float32'';你甚至可以看到尝试

使用错误的类型会让代码的其余部分变得很难写。

这是C告诉你的方式'朝着错误的方向前进。


-Arthur


2004-07-08,亚瑟J. O'Dwyer< aj*@nospam.andrew.cmu.edu>写道:于星期四,2004年7月8日,杰森卷曲写道:
的typedef unsigned long int类型UINT32;
的typedef浮浮点32;
UINT32 myArray的[100];

U32 = ConvertToFloat(3.14159);


*(浮点32 *)U32 = 3.14159;


Nit:

*(Float32 *)& u32 = 3.14159;


- James




" Jason Curl" < J。**** @ motorola.com>在消息中写道

新闻:cc ********** @ newshost.mot.com ...

问候,
我有一个包含32个值的数组。这使得根据单独的枚举提供的索引访问此数组中的元素非常快。该
数组定义为unsigned long int类型。我有一个typedef:

typedef unsigned long int Uint32;
typedef float Float32;
Uint32 myArray [100];

在C ++中有是铸造操作员,但是我正在用C编写我的软件。
我正在试图告诉编译器将值解释为float(此平台上的32位
实体),但是将它存储到Uint32数组中,稍后我需要b $ b转换回浮点数。 (我不想进行类型推广或降级,只需让编译器改变其对类型的解释)。

这可以通过
Uint32 u32;
Float32 f32;

f32 = 3.1415;
u32 = *(Uint32 *)(& f32);

所以现在,如果您将u32打印到控制台,它将打印看起来像垃圾(绝对不是数字3),但二进制表示
保持不变。

相同也可以用联盟结构来完成(但我认为上面的内容更好)。

但为了便于阅读,我想把所有这些结合起来,一起出现在一条线上无需先将数字放入变量
或注册,以便:

u32 = ConvertToFloat(3.14159);

我不想写一个函数 - 开销很高,我现在需要把这个函数放在一个需要链接的库中(这个被多个二进制文件使用,有点像"插件"建筑)。此外,
它依赖于编译器,如果这样的函数是内联的或不是
(据我所知,C并不提供内联,我仍然希望所有内容都在
a头文件,如果可能的话)。在
头文件中有一个宏可以做到这一点很方便。

有谁知道怎么写这样的宏?

有什么东西吗?可以用Microsoft Visual
C ++的程序集编写?我对英特尔集会一无所知。

我能想到的另一个替代方案是32位值的数组,
从我明确设置的东西中获取它的类型。 br />




这与多态有什么关系?


Greetings,

I have an array of 32 values. This makes it extremely fast to access
elements in this array based on an index provided by a separate enum. This
array is defined of type "unsigned long int". I have a typedef for this:

typedef unsigned long int Uint32;
typedef float Float32;
Uint32 myArray[100];

In C++ there are casting operators, however I''m writing my software in C.
I''m trying to tell the compiler to interpret a value as a float (32-bit
entity on this platform), but store it into the Uint32 array, which I later
take and convert back to float. (I don''t want to have type promotion or
demotion, just have the compiler change its interpretation of the type).

This can be easily done by

Uint32 u32;
Float32 f32;

f32=3.1415;
u32 = *(Uint32 *)(&f32);

So now, if you print u32 to the console it will print what looks like
garbage (definitely not the number 3), but the binary representation remains
the same.

The same could be done also with Union structs (but the above is nicer I
think).

But for readability I would like to combine all this into something that
appears on one line without having to first put the number into a variable
or register, so that:

u32 = ConvertToFloat(3.14159);

I don''t want to write a function - the overhead is high and I now need to
put this function in a library that will need to be linked (this is being
used by multiple binaries, somewhat like a "plugin" architecture). Also,
it''s dependent on the compiler if such a function would be inline''d or not
(C doesn''t provide inline as far as I know, and I still want everything in a
header file if possible). It would be convenient to have a macro in a header
file that could do this.

Does anyone know how to write such a macro?

Is there something that can be written in assembly for Microsoft Visual C++?
I don''t know anything about Intel Assembly.

The other alternative I can think is an array of 32-bit values that derives
its type from something I explicitly set it to.

解决方案


On Thu, 8 Jul 2004, Jason Curl wrote:


typedef unsigned long int Uint32;
typedef float Float32;
Uint32 myArray[100];

In C++ there are casting operators, however I''m writing my software in C.
C has type-casting operators, too; there just aren''t as many of them
and they aren''t as ugly. :)
I''m trying to tell the compiler to interpret a value as a float (32-bit
entity on this platform), but store it into the Uint32 array, which I later
take and convert back to float. (I don''t want to have type promotion or
demotion, just have the compiler change its interpretation of the type). The same could be done also with Union structs (but the above is nicer I
think).
Much less portable, though. For that matter, why are you using an
array of *integers* to store *float* values?! Just use an array of
floats! It''s not like there''s a penalty for it!
But for readability I would like to combine all this into something that
appears on one line without having to first put the number into a variable
or register, so that:

u32 = ConvertToFloat(3.14159);
*(Float32*)u32 = 3.14159;

or the obvious macro-ization of the above.
I don''t want to write a function - the overhead is high and I now need to
put this function in a library that will need to be linked (this is being
used by multiple binaries, somewhat like a "plugin" architecture). Also,
it''s dependent on the compiler if such a function would be inline''d or not
(C doesn''t provide inline as far as I know, and I still want everything in a
header file if possible). It would be convenient to have a macro in a header
file that could do this.
C99 provides the ''inline'' keyword; in C90 you can write

#ifndef CNVT2FLOAT
#define CNVT2FLOAT
static Uint32 Cnvt2Float(Float32 f) { return *(Uint32*)&f; }
#endif

The other alternative I can think is an array of 32-bit values that derives
its type from something I explicitly set it to.



This statement doesn''t make sense to me. What are you *really* trying
to do, and why? Certainly there''s no practical reason to use ''Uint32''
instead of ''Float32'' in your code; you can even see already that trying
to use the wrong type makes the rest of your code a pain to write.
This is C''s way of telling you you''re headed in the wrong direction.

-Arthur


On 2004-07-08, Arthur J. O''Dwyer <aj*@nospam.andrew.cmu.edu> wrote:

On Thu, 8 Jul 2004, Jason Curl wrote:


typedef unsigned long int Uint32;
typedef float Float32;
Uint32 myArray[100];

u32 = ConvertToFloat(3.14159);



*(Float32*)u32 = 3.14159;



Nit:
*(Float32*)&u32 = 3.14159;

-- James



"Jason Curl" <j.****@motorola.com> wrote in message
news:cc**********@newshost.mot.com...

Greetings,

I have an array of 32 values. This makes it extremely fast to access
elements in this array based on an index provided by a separate enum. This
array is defined of type "unsigned long int". I have a typedef for this:

typedef unsigned long int Uint32;
typedef float Float32;
Uint32 myArray[100];

In C++ there are casting operators, however I''m writing my software in C.
I''m trying to tell the compiler to interpret a value as a float (32-bit
entity on this platform), but store it into the Uint32 array, which I later take and convert back to float. (I don''t want to have type promotion or
demotion, just have the compiler change its interpretation of the type).

This can be easily done by

Uint32 u32;
Float32 f32;

f32=3.1415;
u32 = *(Uint32 *)(&f32);

So now, if you print u32 to the console it will print what looks like
garbage (definitely not the number 3), but the binary representation remains the same.

The same could be done also with Union structs (but the above is nicer I
think).

But for readability I would like to combine all this into something that
appears on one line without having to first put the number into a variable
or register, so that:

u32 = ConvertToFloat(3.14159);

I don''t want to write a function - the overhead is high and I now need to
put this function in a library that will need to be linked (this is being
used by multiple binaries, somewhat like a "plugin" architecture). Also,
it''s dependent on the compiler if such a function would be inline''d or not
(C doesn''t provide inline as far as I know, and I still want everything in a header file if possible). It would be convenient to have a macro in a header file that could do this.

Does anyone know how to write such a macro?

Is there something that can be written in assembly for Microsoft Visual C++? I don''t know anything about Intel Assembly.

The other alternative I can think is an array of 32-bit values that derives its type from something I explicitly set it to.



What in the world does this have to do with polymorphism?


这篇关于C中的多态性(非常基本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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