无效*问题 [英] void* question

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

问题描述

使用void *的主要原因是什么?

我们什么时候应该对输入参数和返回值使用void *?

我们如何转换void *指向我们需要的类型?

Thanx

解决方案

Janice写道:

什么是使用void *的主要原因是什么?


如果你不知道指向的

对象的实际类型,或者不关心它是什么,请使用`void *''或者

希望隐藏它。

我们什么时候应该对输入参数和返回值使用void *?


当参数和返回值都符合上述一个或多个标准时,

我们怎样才能投出void *指向我们需要的类型的指针?




投射任何东西的唯一方法是使用强制转换操作符。

转换`void *''到'int *'',比方说,使用`(int *)voidptr''。


也许你会给出一些更详细的信息'' s

让你感到困惑,我能够更清楚地解释。


-
Er ********* @ sun.com


Eric Sosman< er ********* @ sun.com>写道:

Janice写道:

我们如何将void *指针转换为我们需要的类型?


投射任何东西的唯一方法是使用强制转换操作符。
要将`void *''转换为`int *'',请使用`(int *)voidptr''。




这是真的,但不是很有帮助。施放到void *之前通常不需要
,因为

void *和其他指向对象或不完整类型的指针之间的转换不会

需要演员表。

-

大笔资金往往能解除我可能遇到的任何顾忌。

- Stephan Wilms


Eric Sosman< er ********* @ sun.com>这样说:

也许如果你能详细说明令你困惑的是什么,我就能更清楚地解释一下。 / blockquote>


恕我直言,这听起来像是家庭作业,所以也许你做得很好......


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


What is the major reason for using void*?
When should we use void* for both input arguments and return value?
How can we cast the void* pointer to the type we need?
Thanx

解决方案

Janice wrote:

What is the major reason for using void*?
Use a `void*'' when you do not know the pointed-to
object''s actual type, or do not care what it is, or
wish to conceal it.
When should we use void* for both input arguments and return value?
When both the arguments and the returned value meet
one or more of the criteria above.
How can we cast the void* pointer to the type we need?



The only way to cast anything is with a cast operator.
To convert a `void*'' to an `int*'', say, use `(int*)voidptr''.

Perhaps if you''d give a little more detail about what''s
puzzling you, I''d be able to explain more clearly.

--
Er*********@sun.com


Eric Sosman <er*********@sun.com> writes:

Janice wrote:

How can we cast the void* pointer to the type we need?



The only way to cast anything is with a cast operator.
To convert a `void*'' to an `int*'', say, use `(int*)voidptr''.



This is literally true, but not very helpful. It is not usually
necessary to cast to or from void * because conversions between
void * and other pointers to object or incomplete type do not
require casts.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms


Eric Sosman <er*********@sun.com> spoke thus:

Perhaps if you''d give a little more detail about what''s
puzzling you, I''d be able to explain more clearly.



IMHO it sounds like homework, so maybe you did just fine...

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


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

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