你能为我解释一下这里的 typedef 是做什么的以及目的是什么吗?
class C
{
public:
...
typedef bool (C::*implementation_defined_bool_type)(bool) const;
operator implementation_defined_bool_type() const {
return _spi ? &C::isPersistent : 0;
}
};
最佳答案
Can you explain for me what the
typedef
is doing here?
typedef bool (C::*implementation_defined_bool_type)(bool) const;
typedef
是一个C
类型的指向常量成员函数的指针,它接受一个bool
作为输入参数并返回一个 bool
。
同时,
operator implementation_defined_bool_type() const
接收C
类型的对象并返回implementation_define_bool_type
类型。
它被称为转换运算符。
what is the purpose of it?
它实现了“Safe Bool Idiom”,旨在验证 bool 上下文中的对象。
请注意 Safe Bool Idiom is obsolete符合 C++11 标准。
关于c++ - 奇怪的 bool 重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11098971/