2012-06-19 51 views
2

你能解释一下这里的typedef在做什么和目的是什么?奇怪的布尔重载

class C 
{ 
    public: 
     ... 

     typedef bool (C::*implementation_defined_bool_type)(bool) const; 

     operator implementation_defined_bool_type() const { 
      return _spi ? &C::isPersistent : 0; 
     } 

}; 
+2

这是过时[ “安全布尔” 成语]的实现(http://stackoverflow.com/questions/6242768/is-the-safe-bool-idiom -obsolete-在-C11)。 –

回答

5

你能为我解释一下什么是typedef在这里做什么?

typedef bool (C::*implementation_defined_bool_type)(bool) const; 

typedef是个指针类型C,这需要一个bool作为输入参数,并且还返回一个bool的const成员函数

虽然

operator implementation_defined_bool_type() const  

注意到在C类型的对象,并返回一个类型implementation_defined_bool_type
它被称为转换运算符

它的目的是什么?

它实现了“安全布尔成语”,其目的是在布尔上下文验证的对象。
请注意,Safe Bool Idiom is obsolete与C++ 11标准。

良好阅读:
The Safe Bool Idiom

+0

它没有解释它实际上做了什么,为什么它以这种方式实现,这是目的和所有。 – Nawaz

+0

@Nawaz:希望它现在。 –

+0

+1。它肯定会。 – Nawaz