等长的 C++ 数组成员(初始化)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/478967/
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
C++ Array Member of Constant Length (Initialisation of)
提问by Rae
I have a class that contains an array. I want this array to be set at the length of a constant:
我有一个包含数组的类。我希望将此数组设置为常量的长度:
// Entities.h
class Entities
{
private:
const int maxLimit;
int objects[maxLimit];
int currentUsage;
public:
Entities();
bool addObject(int identifier);
void showStructure();
};
The main problem I'm having is with the constructor. I thought:
我遇到的主要问题是构造函数。我想:
// Entities.cpp
Entities::Entities() : maxLimit(50)
{
currentUsage = 0;
cout << "Entities constructed with max of 50" << endl;
}
would have been sufficient...but not so. I don't know if I can use the initialiser list for array initialisation.
本来就足够了……但并非如此。我不知道是否可以使用初始化列表进行数组初始化。
How can I initialise the objects
array using the maxLimit
const? I'm relatively new to classes in C++ but I have experience with Java. I'm mainly testing out this phenomenon of 'constness'.
如何objects
使用maxLimit
const初始化数组?我对 C++ 中的类比较陌生,但我有 Java 的经验。我主要是测试这种“恒常”现象。
回答by Johannes Schaub - litb
The array must have a constant length. I mean a length that is the same for all objects of that class. That is because the compiler has to know the size of each object, and it must be the same for all objects of that particular class. So, the following would do it:
该数组必须具有恒定长度。我的意思是该类的所有对象的长度都相同。这是因为编译器必须知道每个对象的大小,并且该特定类的所有对象的大小必须相同。所以,以下将做到这一点:
class Entities
{
private:
static const int maxLimit = 50;
int objects[maxLimit];
int currentUsage;
public:
Entities();
bool addObject(int identifier);
void showStructure();
};
And in the cpp file:
在 cpp 文件中:
const int Entities::maxLimit;
I prefer to use an enumeration for that, because i won't have to define the static in the cpp file then:
我更喜欢为此使用枚举,因为我不必在 cpp 文件中定义静态:
class Entities
{
private:
enum { maxLimit = 50 };
int objects[maxLimit];
int currentUsage;
public:
Entities();
bool addObject(int identifier);
void showStructure();
};
If you want to have a per-object size of the array, then you can use a dynamic array. vector
is such one:
如果您想拥有数组的每个对象大小,那么您可以使用动态数组。vector
是这样的:
class Entities
{
private:
const int maxLimit;
std::vector<int> objects;
int currentUsage;
public:
Entities();
bool addObject(int identifier);
void showStructure();
};
// Entities.cpp
Entities::Entities(int limit)
: maxLimit(limit), objects(limit), currentUsage(0)
{
cout << "Entities constructed with max of 50" << endl;
}
Best is to do as much initialization in the initialization list as possible.
最好是在初始化列表中进行尽可能多的初始化。
回答by Evgeny Lazin
You can use template argument if you need to set array size at compile time:
如果需要在编译时设置数组大小,可以使用模板参数:
template<size_t maxLimit>
class Entities
{
int objects[maxLimit];
public:
Entities() {}
...
};
Entities<1000> inst;
回答by John Boker
to dynamically allocate the memory you may need to use the 'new' keyword like
要动态分配内存,您可能需要使用“new”关键字,例如
objects would be defined like:
对象将被定义为:
int * objects;
inside the constructor you would do:
在构造函数中,您将执行以下操作:
objects = new int [maxLimit];
edit:
编辑:
forgot to mention, you'll need to deallocate the array when you're done, probably in the destructor of the class.
忘了提,完成后您需要释放数组,可能是在类的析构函数中。
delete[] objects;
回答by sykora
const int
s have to be initialized at declaration. If you don't know the value that it has to be at the time of declaration, you're going to have to adopt a different strategy.
const int
s 必须在声明时初始化。如果您在声明时不知道它必须具有的价值,您将不得不采用不同的策略。
You'll need to create the array in the constructor, while keeping a pointer outside. Is this what you want to do?
您需要在构造函数中创建数组,同时在外部保留一个指针。这是你想做的吗?
In your class :
在你的课上:
private:
int maxLimit;
int* objects;
And outside:
和外面:
Entities::Entities() : maxLimit(50)
{
currentUsage = 0;
cout << "Entities constructed with max of 50" << endl;
objects = new int[maxLimit];
}
Entities::~Entities()
{
delete [] objects;
}
回答by MSalters
If all objects have the same length, then length can be static. This makes it a constant integral expression allowed as an array bound:
如果所有对象都具有相同的长度,则长度可以是静态的。这使它成为允许作为数组绑定的常量积分表达式:
class Entities
{
private:
static const int maxLimit = 50;
int objects[maxLimit];
int currentUsage;
//...
};
Remember that sizeof(Entities) is a valid expression. Each Entities object has that same size.
请记住 sizeof(Entities) 是一个有效的表达式。每个实体对象都具有相同的大小。
回答by Beno?t
Use std::vector and you get the expected behaviour. No need to worry about pointers, copies, etc
使用 std::vector 并获得预期的行为。无需担心指针、副本等
#include <vector>
class Entities
{
private:
const int limit;
std::vector<int> objects;
public:
Entities(int a_limit)
: limit(a_limit), objects(a_limit)
{ }
void addObject(int identifier)
{
if (objects.size() == limit)
throw whatever;
objects.push_back(identifier);
}
};