c++ - 关于 C++ 中结构的模板

标签 c++ struct

方法一:

 template <class T>
 struct node{
      T data;
      struct node *next;
 };

方法二:

 template <class T>
 struct node{
      T data;
      struct node<T> *next; // different from above
 };

对于链表,它们都可以正确编译和运行。

有区别吗?如果不是,为什么?

最佳答案

没有区别。

之所以如此,是因为为了方便起见,在模板化的类/结构中,该类/结构的裸名指的是当前模板实例化。

如果您有六个模板化参数,而这些模板化参数本身也有模板化参数,而您只想声明一个指针,那么此功能会变得非常方便:-)

关于c++ - 关于 C++ 中结构的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21836726/

相关文章:

python - 如何使用 Python 解析 JPEG 文件中的复杂结构?

C 错误, "expected declaration specifier"

c++ - GLSL 类型不一致

c++ - 无法使用 fopen() 在 Windows 7 的 C 盘中创建文件

c++ - std::labs() 到底是为了什么?

c - 何时为结构内的数组使用 malloc() 内存

c - 为什么结构体中的变量没有被分配任何值?

swift - 协议(protocol)仅由 struct 或 immutable 实现

c++ - 为什么 C++ ifstream 不能从设备读取?

c++ - QMake 为 32 位构建使用了错误的目录