讨论nullptr和NULL
最近玩了两天的c++11,其中有一个是新的关键字nullptr。 这里讨论下nullptr的存在价值,应用场景以及不支持c++11的时候如何模拟nullptr。
1、为什要有nullptr
我们给一个指针赋初值的时候一般这么写 FILE* fp = NULL;
这里有个NULL的定义,一般情况下它是这么定义的:
constclass nullptr_t_t{public: template<class T> operator T*() const {return 0;} template<class C, class T> operator T C::*() const { return 0; }private: void operator& () const;} nullptr_t = {};#undef NULL#define NULL nullptr_t