何时使用null以及何时使用static_cast< some_pointer_type>(0)? [英] When to use null and when to use static_cast<some_pointer_type>(0)?

查看:75
本文介绍了何时使用null以及何时使用static_cast< some_pointer_type>(0)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些代码使用static_cast< some_pointer_type>(0)而不是NULL

来描述空指针。我想知道

单程的优点和缺点是什么。有什么理由说我们应该对另一个经文。

I see some code use static_cast<some_pointer_type>(0) instead of NULL
to describe null pointer. I''m wondering what is the pros and cons of
each way. Is there any reason why we should one verses the other.

推荐答案

Pe ******* @ gmail.com 写道:
Pe*******@gmail.com wrote:
我看到一些代码使用static_cast< some_pointer_type>(0)而不是NULL
来描述空指针。我想知道每种方式的优点和缺点是什么。有什么理由说我们应该用另一个来解释。
I see some code use static_cast<some_pointer_type>(0) instead of NULL
to describe null pointer. I''m wondering what is the pros and cons of
each way. Is there any reason why we should one verses the other.




复杂的方法是什么代码?你可以发一个样本吗?


NULL是神奇的,通常应该总是显示为常量NULL。

也许你看到了从中升级的代码C,使用(void *)0。


NULL是神奇的,因为在C ++中常量0总是可以自由地转换为任何

指针类型,只是就像你已经精心投入它一样安全。

不要。


-

Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!



What code does it the complex way? Could you post a sample?

NULL is magic, and should generally always appear as the constant "NULL".
Maybe you saw code that upgraded from C, which used (void*)0.

NULL is magic because a constant 0, in C++, always freely converts to any
pointer type, just as typesafely as if you had put an elaborate cast on it.
Don''t.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!




Phlip写道:

Phlip wrote:
Pe ******* @ gmail.com 写道:
Pe*******@gmail.com wrote:
我看到一些代码使用static_cast< some_pointer_type> (0)而不是NULL
来描述空指针。我想知道每种方式的优点和缺点是什么。有什么理由说我们应该用另一个来解释它。
复杂的方法是什么代码?你可以发一个样品吗?
I see some code use static_cast<some_pointer_type>(0) instead of NULL
to describe null pointer. I''m wondering what is the pros and cons of
each way. Is there any reason why we should one verses the other.
What code does it the complex way? Could you post a sample?




模板< class T>

class Nil {

public:

运算符T *(){return static_cast< T *>(0); }

};


模板< class T>

void删除(T *& x){

删除x;

x = Nil< T>();

}


以上是代码片段。


一般来说,NULL是首选,对吧?

NULL是神奇的,通常应该总是显示为常量NULL。也许你看到从C升级的代码,它使用了(void *)0。

NULL是神奇的,因为在C ++中,常量0总是可以自由地转换为任何指针类型,就像你对它进行了精心设计一样安全。
不要。

-
Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!



template <class T>
class Nil {
public:
operator T* () { return static_cast<T*>(0); }
};

template <class T>
void Delete(T*& x) {
delete x;
x = Nil<T>();
}

The above is the code fragment.

In general, NULL is preferred, right?

NULL is magic, and should generally always appear as the constant "NULL".
Maybe you saw code that upgraded from C, which used (void*)0.

NULL is magic because a constant 0, in C++, always freely converts to any
pointer type, just as typesafely as if you had put an elaborate cast on it.
Don''t.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!







Pe ****** *@gmail.com 写道:
Phlip写道:
Pe ******* @ gmail.com 写道:
Pe*******@gmail.com wrote:
I看一些代码使用static_cast< some_pointer_type>(0)而不是NULL
来描述空指针。我想知道每种方式的优点和缺点是什么。有什么理由说我们应该用另一个来解释。
I see some code use static_cast<some_pointer_type>(0) instead of NULL
to describe null pointer. I''m wondering what is the pros and cons of
each way. Is there any reason why we should one verses the other.



它的复杂方式是什么代码?您可以发布样本吗?



What code does it the complex way? Could you post a sample?



模板< class T>
类Nil {
public:
operator T *(){return static_cast< T *>(0); }



template <class T>
class Nil {
public:
operator T* () { return static_cast<T*>(0); }




该演员表无效,幸运的是,这是不必要的。


你不能在不相关的类型之间静态播放。一个int和一个T *是完全无关的
。幸运的是,0是神奇的,它可以是任何

指针以及积分。所以,这只是从T *到T *调用静态转换

...如果代码不能编译的话。


原始编码员可能想要的是重新诠释演员。

然而,由于静态演员的工作,它表明它不是必要的,而是完全不需要任何演员。



That cast is invalid and, luckily, unnecissary.

You can''t static_cast between unrelated types. An int and a T* are
totally unrelated. Luckily enough, 0 is magic in that it can be any
pointer as well as an integral. So, that is just calling a static cast
from T* to T*...if it wasn''t the code would not compile.

What the original coder probably intended was a reinterpret cast.
However, since the static cast worked it shows that it is not necissary
to perform any casting at all.


这篇关于何时使用null以及何时使用static_cast&lt; some_pointer_type&gt;(0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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