什么是铸造一个空指针的原因是什么? [英] What are the reasons for casting a void pointer?

查看:162
本文介绍了什么是铸造一个空指针的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习从头C ++,因此我没有C的专家了解在C ++中,你不能一个空指针转换为任何别的东西,我明白背后的原因。但是,我知道,在C,就可以了。什么是这种情况的可能原因?这似乎只是它是类型安全一个巨大的窟窿,这(对我来说)似乎是一件坏事。

I'm learning C++ from scratch, and as such I don't have an expert understanding of C. In C++, you can't cast a void pointer to whatever, and I understand the reasons behind that. However, I know that in C, you can. What are the possible reasons for this? It just seems like it's be a huge hole in type safety, which (to me) seems like a bad thing.

推荐答案

您可以施放一个无效* 中的两个的语言另一个指针。也许你含蓄的意思。

You can cast a void* to another pointer in both languages. Perhaps you meant implicitly.

这是非常方便的在C不必是明确了。在C ++中,我们有模板,所以编写普通code没有必要为无效* 铸造和诸如此类的东西。在C语言中没有选择的余地。一个普通的容器必须持有无效* 的对象,而且它更容易反复说 MYDATA * D =节点; 那么它是 MYDATA * D =(MYDATA *)节点。

It's very convenient in C to not have to be explicit about it. In C++ we have templates, so to write generic code there's no need for void* casting and whatnot. In C there is no choice. A generic container has to hold void* to objects, and it's much easier to repeatedly say mydata* d = node; then it is mydata* d = (mydata*)node;.

所以这是pretty就像你说的。在C型一般安全性在C确实没有得到一样的重视++,特别是当它来到无效* ,因为它被假设是一个简单的通用指针随你。有没有必要在C ++中,所以最好还是让它当你处理它明确。

So it's pretty much like you said. In C type safety in general didn't receive as much emphasis as it did in C++, especially when it came to void* because it was suppose to be a simple generic pointer to whatever. There's no need for that in C++, so better make it explicit when you're dealing with it.

这篇关于什么是铸造一个空指针的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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