2015一月13
DUI-unspecified_bool_type把类对象做bool判断
unspecified_bool_type把类对象做bool判断,是依赖于它自身的一个变量来定义的
比如我们随便写个类:
class Base { public: int i; };
再写个测试
Base base;if (base){}
肯定报错,但这样写就不会了
class Base { public: typedef int Base::*unspecified_bool_type; operator unspecified_bool_type() const { return i ? &Base::i : NULL; } public: int i; };
单步,你会发现bool判断走到了unspecified_bool_type中
DMLazyT使用了相似的代码:
/// ------------------------------------------------- /// @brief unspecified_bool_type /// @return 增加bool值判定 typedef TPtr DMLazyBaseT::*unspecified_bool_type; operator unspecified_bool_type() const { return m_ptr ? &DMLazyBaseT::m_ptr : NULL; }
文章作者:hgy413
本文地址:https://hgy413.com/1958.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!