C++ 奇异布尔过载
你能给我解释一下这里的typedef是做什么用的吗C++ 奇异布尔过载,c++,C++,你能给我解释一下这里的typedef是做什么用的吗 class C { public: ... typedef bool (C::*implementation_defined_bool_type)(bool) const; operator implementation_defined_bool_type() const { return _spi ? &C::isPersistent : 0;
class C
{
public:
...
typedef bool (C::*implementation_defined_bool_type)(bool) const;
operator implementation_defined_bool_type() const {
return _spi ? &C::isPersistent : 0;
}
};
你能给我解释一下typedef
在这里做什么吗?
typedef
s一个指针,指向C
类型的常量成员函数,该函数将bool
作为输入参数,并返回bool
而
operator implementation_defined_bool_type() const
接收类型为C
的对象,并返回类型为实现定义的类型
它被称为转换运算符 它的目的是什么? 它实现了“安全布尔惯用法”,目的是在布尔上下文中验证对象。
请注意,使用C++11标准 读得好:
这是一个过时的实现。它没有解释它实际上做了什么,为什么要这样实现,它的目的和全部。
operator implementation_defined_bool_type() const