C++ 如何实例化对象的静态向量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7531981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to instantiate a static vector of object?
提问by Michael
I have a class A, which has a static vector of objects. The objects are of class B
我有一个 A 类,它有一个对象的静态向量。对象属于 B 类
class A {
public:
static void InstantiateVector();
private:
static vector<B> vector_of_B;
}
In function InstantiateVector()
在函数 InstantiateVector() 中
for (i=0; i < 5; i++) {
B b = B();
vector<B>.push_back(b);
}
But I have compilation error using visual studio 2008: unresolved external symbol... Is it possible to instantiate static vector using above method? For object b to be created, some data has to be read from input file, and stored as member variables of b
但是我使用 Visual Studio 2008 时出现编译错误:未解析的外部符号...是否可以使用上述方法实例化静态向量?要创建对象 b,必须从输入文件中读取一些数据,并将其存储为 b 的成员变量
Or it is not possible, and only simple static vector is possible? I read somewhere that to instantiate static vector, you must first define a const int a[] = {1,2,3}, and then copy a[] into vector
或者这是不可能的,只有简单的静态向量是可能的?我在某处读到要实例化静态向量,您必须首先定义一个 const int a[] = {1,2,3},然后将 a[] 复制到向量中
回答by Chad
You have to provide the definition of vector_of_b
as follows:
您必须提供vector_of_b
如下定义:
// A.h
class A {
public:
static void InstantiateVector();
private:
static vector<B> vector_of_B;
};
// A.cpp
// defining it fixes the unresolved external:
vector<B> A::vector_of_B;
As a side note, your InstantiateVector()
makes a lot of unnecessary copies that may (or may not) be optimized away.
作为旁注,您InstantiateVector()
制作了许多可能(或可能不会)优化掉的不必要的副本。
vector_of_B.reserve(5); // will prevent the need to reallocate the underlying
// buffer if you plan on storing 5 (and only 5) objects
for (i=0; i < 5; i++) {
vector_of_B.push_back(B());
}
In fact, for this simple example where you are just default constructing B
objects, the most concise way of doing this is simply to replace the loop all together with:
事实上,对于这个只是默认构造B
对象的简单示例,最简洁的方法是简单地将循环全部替换为:
// default constructs 5 B objects into the vector
vector_of_B.resize(5);
回答by Kerrek SB
Add the definitionof the static member object to your class implementation file:
将静态成员对象的定义添加到您的类实现文件中:
#include "A.h"
std::vector<B> A::vector_of_B;
Or rather, absorbing and obviating the initialization routine:
或者更确切地说,吸收和消除初始化例程:
std::vector<B> A::vector_of_B(5, B()); // same effect as `A::InstantiateVector()`
Note: In C++98/03, vector_of_B(5)
and vector_of_B(5, B())
are identical. In C++11 they're not.
注意:在 C++98/03 中,vector_of_B(5)
和vector_of_B(5, B())
是相同的。在 C++11 中,它们不是。
回答by Gob00st
You can either use a static helper or use boost::assign
您可以使用静态助手或使用 boost::assign
1>using a small helper:
1>使用小帮手:
#include <boost\assign.hpp>
class B{};
class A {
public:
static bool m_helper;
static bool InstantiateVector()
{
for (int i=0; i < 5; ++i)
{
B b;
vector_of_B.push_back(b);
}
return true;
}
private:
static vector<B> vector_of_B;
};
bool A::m_helper = InstantiateVector();//helper help initialize static vector here
2> Use boost::assign which is eaiser, one line is enough:
2> 使用更简单的 boost::assign ,一行就够了:
vector<B> A::vector_of_B = boost::assign::list_of(B())(B())(B())(B())(B());