如何访问模板成员 class

How to access members of a template class

在 C++ 中,向量的工作原理是,如果我实例化一个向量,例如 Obj 类型,我可以通过指定元素使用点访问器访问 Obj 的成员。我正在尝试使用模板 <class T> struct.

做同样的事情

如何访问结构的 Obj 成员,例如 template <class T> Struct 包含 vector<T>,实例化为 Struct<Obj>

如果我走错了路,还有什么选择?

看起来你有这个:

struct Obj
{
    int x_;
};

template <typename T>
struct Struct
{
    std::vector<T> items_;
};

因此要访问一个元素(假设您将它添加到向量中):

int main()
{
    Struct s;
    s.items_.resize(10);
    s.items_[0].x_ = 5; // Access the object memeber of element 0 in the vector.
}