c++ - 使用模板类定义声明变量内联

标签 c++

我有一个这样的链表结构:

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/

相关文章:

c++ - 使用链接列表插入 Pop 功能

c++ - 如何将宽字符串转换为 ASCII

c++ - 在成员函数尾随返回类型中使用 this 和属性?

c++ - Boost.Asio async_send 问题

C++ 重复符号

c++ - 如何获得最大数量的多纹理单元

c++ - 具有随机访问权限的列表的 STL 容器?

c++ - Cuda 类型双关语 - memcpy vs UB union

c++ - 多线程作业队列管理器

c# - 遍历从 C# 传递的未知 C 结构