Before C++11, the only way to refer to a null pointer was by using the integer literal
0, which created ambiguity with regard to whether a pointer or an integer was intended. Even with the
NULL
macro, the underlying value is still
0.
C++11 introduced the keyword
nullptr, which is unambiguous and should be used systematically.
Noncompliant Code Example
- void f(char *c);
- void g(int i);
- void h()
- {
- f(0); // Noncompliant
- f(NULL); // Noncompliant
- g(0); // Compliant, a real integer
- g(NULL); // Noncompliant, NULL should not be used for a real integer
- }
复制代码 Compliant Solution
- void f(char *c);
- void g(int i);
- void h()
- {
- f(nullptr); // Compliant
- g(0); // Compliant, a real integer
- }
复制代码 See
- C++ Core Guidelines ES.47
- Use nullptr rather than 0 or NULL
For example
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|