Is nullptr in C++ the same as null in C#?
c++中的nullptr和c#中的null一样吗?
Seems like no one asked this question on Google or Stackflow.
似乎没有人在谷歌或Stackflow上问过这个问题。
3 个解决方案
#1
13
In general yes, both assign the null value to pointer like types
一般来说,都将空值赋给指针类型
- C#: class, interface, delegate or pointer types
- 类、接口、委托或指针类型
- C++: pointer types or pointer-to-member type
- c++:指针类型或指针到成员类型
It doesn't convert to numeric values in either language. One difference though is that in C++ nullptr
can convert to bool
. This is not true in C#
它不会在两种语言中转换为数值。一个区别是,在c++中,nullptr可以转换为bool。在c#中不是这样的
#2
4
Yes. In C++, nullptr
is the equivalent of null
in Java, C#, and a variety of other languages. Prior to C++11, it was standard to use NULL
(which was a macro defined as 0); however, nullptr
actually ensures that it can be used only in the context of pointers (whereas NULL
, by virtue of having been defined as 0, can also be used as the return value for a function that returns an int
instead of a pointer, which is counterintuitive), though both nullptr
and NULL
are implicitly convertible to bool
.
是的。在c++中,nullptr在Java、c#和其他各种语言中等同于null。在c++ 11之前,标准使用NULL(宏定义为0);然而,实际上nullptr确保它只可以使用上下文中的指针(而空,由于被定义为0,也可以使用作为一个函数的返回值返回一个int,而不是指针,这是违反直觉的),虽然bool nullptr和零都是隐式转换。
#3
0
Yes, it is the same. It is the invalid pointer.
是的,是一样的。它是无效指针。
#1
13
In general yes, both assign the null value to pointer like types
一般来说,都将空值赋给指针类型
- C#: class, interface, delegate or pointer types
- 类、接口、委托或指针类型
- C++: pointer types or pointer-to-member type
- c++:指针类型或指针到成员类型
It doesn't convert to numeric values in either language. One difference though is that in C++ nullptr
can convert to bool
. This is not true in C#
它不会在两种语言中转换为数值。一个区别是,在c++中,nullptr可以转换为bool。在c#中不是这样的
#2
4
Yes. In C++, nullptr
is the equivalent of null
in Java, C#, and a variety of other languages. Prior to C++11, it was standard to use NULL
(which was a macro defined as 0); however, nullptr
actually ensures that it can be used only in the context of pointers (whereas NULL
, by virtue of having been defined as 0, can also be used as the return value for a function that returns an int
instead of a pointer, which is counterintuitive), though both nullptr
and NULL
are implicitly convertible to bool
.
是的。在c++中,nullptr在Java、c#和其他各种语言中等同于null。在c++ 11之前,标准使用NULL(宏定义为0);然而,实际上nullptr确保它只可以使用上下文中的指针(而空,由于被定义为0,也可以使用作为一个函数的返回值返回一个int,而不是指针,这是违反直觉的),虽然bool nullptr和零都是隐式转换。
#3
0
Yes, it is the same. It is the invalid pointer.
是的,是一样的。它是无效指针。