在C中将NULL定义为void*指针值为0
#define NULL (void*)0
#ifndef NULL #ifdef __cplusplus #define NULL 0 #else #define NULL ((void *)0) #endif #endif
#include <iostream>
#include <typeinfo>
using namespace std;
void f(int i)
{
cout << "f(int)" << endl;
}
void f(void* ptr)
{
cout << "f(void*)" << endl;
}
int main()
{
f(NULL);
return 0;
}
输出结果:
nullptr取代了有错误倾向的NULL,nullptr可以被理解为指向NULL的指针
#include <iostream>
#include <typeinfo>
using namespace std;
void f(int i)
{
cout << "f(int)" << endl;
}
void f(void* ptr)
{
cout << "f(void*)" << endl;
}
int main()
{
//f(NULL);
f(0); //输出为f(int)
f(nullptr); //输出为f(void*)
return 0;
}const char *pc = str.c_str(); if (pc != nullptr) cout << pc << endl; void (*func)() = nullptr;
不能将nullptr赋值给整形
int i1 = nullptr; //error
if (i1 == nullptr){} //error
if (nullptr){} //error
nullptr = 0; //error
C++11 新特性之 nullptr,布布扣,bubuko.com
原文:http://blog.csdn.net/aspnet_lyc/article/details/30827073