无效*操作 [英] void* operations

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

问题描述



我没有做过很多C ++编程(在C中做了很多)和

我之所以考虑的原因切换现在也是我在这里张贴的问题。


我有一些库函数可以返回给我一个无效的数据

已由用户指定。还有一些函数返回

枚举,其中包含有关此数据的数据类型的信息。所以我以前做过的
是基于

数据类型枚举的switch case语句,然后显式地将void *重新转换为相应的

数据类型。


现在我有一个案例,我有两个值,每个值可以是任何

数据类型。让我们说我必须倍增他们。因此,如果我要使用我的

常用方法,我会有一个带有numDataType(C)2

(组合)情况的switch case语句。 C ++有更好的方法从中提取数据

void *?


感谢您的帮助,

Ashish。

解决方案

作为*************** @ gmail.com 写道:

我还没有完成很多C ++编程(完成了很多它在C)和
我正在考虑切换的原因也是我在这里张贴的问题。

我有一些库函数,返回给我一个无效*的数据
已由用户指定。还有一些函数返回
枚举,其中包含有关此数据的数据类型的信息。所以我过去所做的是基于数据类型枚举的切换案例陈述,然后明确地将void *重新转换为适当的数据类型。

现在我有一个案例,我有两个值,每个值都可以是任何数据类型。让我们说我必须倍增他们。因此,如果我要使用我的常用方法,我将使用带有numDataType(C)2
(组合)情况的switch case语句。 C ++是否有更好的方法从void *中提取数据?




更好在什么意义上?


V

-

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


作为********* ******@gmail.com 写道:


我没有做过很多C ++编程(在C中做了很多)我正在考虑转换的原因也是我在这里张贴的问题。

我有一些库函数给我一个空白*
已由用户指定的数据。还有一些函数返回
枚举,其中包含有关此数据的数据类型的信息。所以我过去所做的是基于
数据类型枚举的switch case语句,然后明确地将void *重新转换为适当的
数据类型。 br />
现在我有一个案例,我有两个值,每个值都可以是任何数据类型。让我们说我必须倍增他们。因此,如果我要使用我的常用方法,我将使用带有numDataType(C)2
(组合)情况的switch case语句。 C ++有更好的方法从
void *中提取数据吗?



是的,不要!


返回a指向基类型的指针,并从

派生所有数据类。从切换到多态的重构案例。


类基础

{

virtual~base(){}

...

};


class Data1:public Base

{

....

};


....


-

Ian Collins。




Victor Bazarov写道:

作为*************** @ gmail.com 写道:

我没有做过很多C ++编程(在C语言中完成了很多)和
我正在考虑切换的原因也是我的问题。 />在这里发帖。

我有一些库函数,它们返回给我一个无效的数据
已由用户指定。还有一些函数返回
枚举,其中包含有关此数据的数据类型的信息。所以我过去所做的是基于数据类型枚举的切换案例陈述,然后明确地将void *重新转换为适当的数据类型。

现在我有一个案例,我有两个值,每个值都可以是任何数据类型。让我们说我必须倍增他们。因此,如果我要使用我的常用方法,我将使用带有numDataType(C)2
(组合)情况的switch case语句。 C ++是否有更好的方法从void *中提取数据?



更好在什么意义上?

V
-
请通过邮件回复从我的地址删除资金




维克多,更好的方式从某种意义上说,我可以定义某种模板或其他类似的东西,这将使我能够使用一个共同的功能,可以通过void *无论

指针实际指向的数据的数据类型。


Ashish。


Hi,
I haven''t done a lot of C++ programming (done a lot of it in C) and the
reason why I''m considering switching now is also the question I''m
posting here.

I have some library functions that return to me a void* to data that
has been specifed by the user. There are also functions that return an
enum which contains information about the data type of this data. So
what I''ve done in the past is made a switch case statement based on the
data type enum and then explicitly re-cast the void* to the appropriate
data type.

Now I have a case where I have two values and each of them could be any
data type. Lets say I have to multiply them. So if I was to use my
usual method I''d have a switch case statement with numDataType(C)2
(combination) cases. Does C++ have a better way of extracting data from
void*?

Thanks for your help,
Ashish.

解决方案

as***************@gmail.com wrote:

I haven''t done a lot of C++ programming (done a lot of it in C) and
the reason why I''m considering switching now is also the question I''m
posting here.

I have some library functions that return to me a void* to data that
has been specifed by the user. There are also functions that return an
enum which contains information about the data type of this data. So
what I''ve done in the past is made a switch case statement based on
the data type enum and then explicitly re-cast the void* to the
appropriate data type.

Now I have a case where I have two values and each of them could be
any data type. Lets say I have to multiply them. So if I was to use my
usual method I''d have a switch case statement with numDataType(C)2
(combination) cases. Does C++ have a better way of extracting data
from void*?



"Better" in what sense?

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


as***************@gmail.com wrote:

Hi,
I haven''t done a lot of C++ programming (done a lot of it in C) and the
reason why I''m considering switching now is also the question I''m
posting here.

I have some library functions that return to me a void* to data that
has been specifed by the user. There are also functions that return an
enum which contains information about the data type of this data. So
what I''ve done in the past is made a switch case statement based on the
data type enum and then explicitly re-cast the void* to the appropriate
data type.

Now I have a case where I have two values and each of them could be any
data type. Lets say I have to multiply them. So if I was to use my
usual method I''d have a switch case statement with numDataType(C)2
(combination) cases. Does C++ have a better way of extracting data from
void*?


Yes, don''t!

Return a pointer to a base type and derive all your data classes from
it. A case of refactoring from switch to polymorphism.

class Base
{
virtual ~base() {}
...
};

class Data1 : public Base
{
....
};

....

--
Ian Collins.



Victor Bazarov wrote:

as***************@gmail.com wrote:

I haven''t done a lot of C++ programming (done a lot of it in C) and
the reason why I''m considering switching now is also the question I''m
posting here.

I have some library functions that return to me a void* to data that
has been specifed by the user. There are also functions that return an
enum which contains information about the data type of this data. So
what I''ve done in the past is made a switch case statement based on
the data type enum and then explicitly re-cast the void* to the
appropriate data type.

Now I have a case where I have two values and each of them could be
any data type. Lets say I have to multiply them. So if I was to use my
usual method I''d have a switch case statement with numDataType(C)2
(combination) cases. Does C++ have a better way of extracting data
from void*?



"Better" in what sense?

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



Victor, "better way" in the sense that will it be possible for me to
define some sort of template or something else that will alow me to
have a common function that can be called with a void* irrespective of
the data type of the data that the pointer actually points to.

Ashish.


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

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