来自维基百科,如果我们的编译器不支持c++11,我们可以自己实现一个,就像下面这样:
const class nullptr_t
{
public:
template<class T>
inline operator T*() const
{ return 0; }
template<class C, class T>
inline operator T C::*() const
{ return 0; }
private:
void operator&() const;
} nullptr = {};
我看不懂上面的代码。
- - - - - - - - - - - 更新 - - - - - - - - -
对不起,我没有表达清楚。
template<class T>
inline operator T*() const
{ return 0; }
template<class C, class T>
inline operator T C::*() const
{ return 0; }
上面的代码,我看不懂语法。我以前从未见过这种模板形式。(如“operator TC::*()”)
template<class T>
inline operator T*() const
{ return 0; }
意味着类型的对象nullptr_t
可以隐式转换为任何(非成员)指针类型。它只是一个为任何类型模板化的operator T *()
函数(转换为类型T *
)T
。
template<class C, class T>
inline operator T C::*() const
{ return 0; }
意味着类型的对象nullptr_t
可以转换为指向C
任何类型(模板参数T
)的任何类(模板参数)的非静态成员的指针。
两个运算符都是const
,因此不能更改nullptr_t
对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句