我有一个这样的链表结构:
template<class T>
class LinkedList {
private:
template<class U>
struct Node {
U data;
Node<U> *link;
};
Node<T> *head;
};
我最想做的是将内部类定义与 head
的声明合并,如:
// Is something like this possible?
template<class U>
struct Node {
U data;
Node<U> *link;
} head; // Put head right here somehow, while also specifying the type parameter.
自从创建 Node<U>
的整点以来结构就是定义head
.是否可以将这两者联系在一起?
最佳答案
只保留占位符 T
并删除不必要的 U
:
template<class T>
class LinkedList {
private:
struct Node {
T data;
Node *link;
};
Node *head;
};
关于c++ - 使用模板类定义声明变量内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19384775/