You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/en-us/02-usability.md
+9-8
Original file line number
Diff line number
Diff line change
@@ -18,20 +18,21 @@ which refers to the language behavior that occurred before the runtime.
18
18
19
19
### nullptr
20
20
21
-
The purpose of `nullptr` appears to replace `NULL`. In a sense,
22
-
traditional C++ treats `NULL` and `0` as the same thing,
23
-
depending on how the compiler defines NULL,
24
-
and some compilers define NULL as `((void*)0)` Some will define it directly as `0`.
21
+
The purpose of `nullptr` appears to replace `NULL`. There are **null pointer constants** in the C and C++ languages,
22
+
which can be implicitly converted to null pointer value of any pointer type,
23
+
or null member pointer value of any pointer-to-member type in C++.
24
+
`NULL` is provided by the standard library implementation and defined as an implementation-defined null pointer constant.
25
+
In C, some standard libraries defines `NULL` as `((void*)0)` and some define it as `0`.
25
26
26
-
C++ **does not allow** to implicitly convert `void *` to other types.
27
-
But if the compiler tries to define `NULL` as `((void*)0)`, then in the following code:
27
+
C++ **does not allow** to implicitly convert `void *` to other types, and thus `((void*)0)` is not a valid implementation
28
+
of `NULL`. If the standard library tries to define `NULL` as `((void*)0)`, then compilation error would occur in the following code:
28
29
29
30
```cpp
30
31
char *ch = NULL;
31
32
```
32
33
33
34
C++ without the `void *` implicit conversion has to define `NULL` as `0`.
34
-
This still creates a new problem. Defining `NULL` to 0 will cause the overloading feature in `C++` to be confusing.
35
+
This still creates a new problem. Defining `NULL` to `0` will cause the overloading feature in `C++` to be confusing.
35
36
Consider the following two `foo` functions:
36
37
37
38
```cpp
@@ -41,7 +42,7 @@ void foo(int);
41
42
42
43
Then the `foo(NULL);` statement will call `foo(int)`, which will cause the code to be counterintuitive.
43
44
44
-
To solve this problem, C++11 introduced the `nullptr` keyword, which is specifically used to distinguish null pointers, 0. The type of `nullptr` is `nullptr_t`, which can be implicitly converted to any pointer or member pointer type, and can be compared equally or unequally with them.
45
+
To solve this problem, C++11 introduced the `nullptr` keyword, which is specifically used to distinguish null pointers, `0`. The type of `nullptr` is `nullptr_t`, which can be implicitly converted to any pointer or member pointer type, and can be compared equally or unequally with them.
45
46
46
47
You can try to compile the following code using clang++:
Copy file name to clipboardExpand all lines: book/zh-cn/02-usability.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,9 @@ order: 2
14
14
15
15
### nullptr
16
16
17
-
`nullptr` 出现的目的是为了替代 `NULL`。在某种意义上来说,传统 C++ 会把`NULL`、`0` 视为同一种东西,这取决于编译器如何定义 `NULL`,有些编译器会将`NULL` 定义为 `((void*)0)`,有些则会直接将其定义为`0`。
17
+
`nullptr` 出现的目的是为了替代 `NULL`。 C 与 C++ 语言中有**空指针常量**,它们能被隐式转换成任何指针类型的空指针值,或 C++ 中的任何成员指针类型的空成员指针值。`NULL` 由标准库实现提供,并被定义为实现定义的空指针常量。在 C 中,有些标准库会把`NULL` 定义为 `((void*)0)` 而有些将它定义为`0`。
18
18
19
-
C++ **不允许**直接将 `void *` 隐式转换到其他类型。但如果编译器尝试把`NULL` 定义为 `((void*)0)`,那么在下面这句代码中:
0 commit comments