[英]Regarding templates for struct in C++
Method 1: 方法1:
template <class T>
struct node{
T data;
struct node *next;
};
Method 2: 方法2:
template <class T>
struct node{
T data;
struct node<T> *next; // different from above
};
Both of them compiles and runs correctly for a linked list. 它们都可以编译并针对链表正确运行。
Isn't there a difference? 没有区别吗? If not, why? 如果没有,为什么?
There is no difference. 没有区别。
The reason that this is so is because, for convenience, within a templated class/struct, the naked name of that class/struct refers to the current template instantiation. 这样做的原因是,为了方便起见,在模板化的类/结构中,该类/结构的裸名是指当前模板实例。
This feature becomes really handy if you have, say, six templated arguments that themselves have templated arguments, and you just want to declare a pointer :-) 如果您有六个模板化参数,而这些模板化参数本身具有模板化参数,而您只想声明一个指针,则此功能非常方便:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.