我看这个CPP闪电谈video 有它显示了在0:37这个typedef是如何工作的?
template<typename T, typename cleanup = QScopedPointerDeleter<T>>
class QScopedPointer{
typedef T *QScopedPointer::*RestrictedBool; // how does this work?
//why not QScopedPointer<T> since QScopedPointer is a template?
public:
inline operator RestrictedBool() const
{
return isNull()? Q_NULLPTR : &QScopedPointer::d;
}
inline bool isNull() const{ return !d;}
protected:
T *d;
};
我有一个很难理解typedef T *QScopedPointer::*RestrictedBool;
这个代码片段,这是什么意思?
我创建了一个类似的类F
,但它不能编译,class QScopedPointer
和class F
这两个typedefs有什么区别?
template<typename T>
class F{
typedef T *F::*bool;
public:
operator bool(){return true;}
};
专家提示:看看使用该类型存储的值:'&QScopedPointer :: d'是指向成员指针的指针。还要注意'typedef'的语法是'typedef'。注意'RestrictedBool'是新名称,在typedef中不能使用'bool'作为新名称。您也不需要在其自己的类定义中使用类模板参数。 –
chris