为什么建议你用nullptr而不是NULL
https://zhuanlan.zhihu.com/p/79883965
在C语言中,NULL实际是一个void *指针
#define NULL ((void *)0)
#if defined(__cplusplus)
# define NULL 0 /* C++中使用0作为NULL的值 */
#else
# define NULL ((void *)0) /* C中使用((void *)0)作为NULL的值 */
#endif
那如果NULL和C中一样是((void *) 0)呢?
//null.cpp
#include<iostream>
int main(void)
{
char p[] = "12345";
int *a = (void*)p; 错误:C++中不能隐式转换成其他指针类型
return 0;
}
你总不能每次都显式转换空指针来初始化吧
隐式转换
https://blog.csdn.net/j497205974/article/details/82690786
void func(int) {}
void func(int *) {}
func(NULL) 会因为NULL为0调用func(int)函数
nullptr并非整型类别,甚至也不是指针类型,但是能转换成任意指针类型。nullptr的实际类型是std:nullptr_t
网友评论