简体   繁体   English

静态向量成员变量

[英]static vector member variable

So I have a class with a static vector member: 所以我有一个带有静态向量成员的类:

class Foo {
// some private members
public:
static vector<Bar> MyVector;
};

Now I initialize MyVector before actually using it: 现在,我在实际使用MyVector之前对其进行了初始化:

std::vector<Bar> Foo::MyVector;

Then I want to define a variable like this in main(), but it doesn't seem to work: 然后我想在main()中定义一个这样的变量,但是它似乎不起作用:

Foo::MyVector::size_type t; 

This workaround does work, but doesn't look beautiful (to my eyes): 这种解决方法确实有效,但是(在我看来)看起来并不漂亮:

vector<Bar>::size_type t;

What is the basic concept that I have been missing here? 我在这里缺少的基本概念是什么?

EDIT 编辑

The main point is, should I always come back to see declaration of MyVector as vector< Bar> before proceeding to declaring a size_type for it? 要点是,在继续声明它的size_type之前,我是否应该总是回来将MyVector声明为vector <Bar>

It is not workaround. 这不是解决方法。 MyVector is object. MyVector是对象。 vector<Bar> is type. vector<Bar>是类型。 size_type is typedef in vector class, not member variable. size_typevector类中是typedef,而不是成员变量。

size_type is a type declared in std::vector . size_type是在std::vector声明的类型。 It is bound to the class, not individual instances. 它绑定到类,而不是单个实例。 Additionally, the :: operator is only used with namespaces and classes. 此外,::运算符仅与名称空间和类一起使用。 You cannot use it with variables. 您不能将其与变量一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM