相关疑难解决方法(0)

在没有模板参数的类模板中使用类名

代码来自C++书籍,如下所示:

为什么这个公共成员Link*next没有typename参数?

template <typename E> class Link {
private:
    static Link<E>* freelist;
public:
    E element;
    Link* next;  // this line confused me....

    Link(const E& elemval, Link* nextval = NULL)
    {
        element = elemval; next = nextval;
    }
    Link(Link* nextval = NULL) { next = nextval; }
    void* operator new(size t){
        if (freelist == NULL) return ::new Link;
        Link<E>* temp = freelist;
        freelist = freelist->next;
        return temp; // Return the link
    }
};
Run Code Online (Sandbox Code Playgroud)

我认为应该是Link<E>* next.

请告诉我它没有模板参数的原因.

c++ templates

11
推荐指数
1
解决办法
1449
查看次数

这种typedef如何工作?

我正在观看这个cpp闪电谈话视频 ,它在0:37显示了这段代码

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;
};
Run Code Online (Sandbox Code Playgroud)

我很难理解typedef T *QScopedPointer::*RestrictedBool;,这是什么意思?

我创建了一个类似的类F,但是它没有编译,两个typedef之间class QScopedPointer和之间有什么区别class F

 template<typename T>
 class F{
  typedef T *F::*bool;
  public:
   operator bool(){return true;}
 };
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
1
解决办法
167
查看次数

标签 统计

c++ ×2

templates ×1