如何在 c++ 中声明数组 标头?
这与其他一些问题有关,例如:这个,以及我的一些其他问题。
在这个问题和其他问题中,我们看到我们可以通过一个不错的步骤来声明和初始化字符串数组,例如:
const char* const list[] = {"zip", "zam", "bam"}; //from other question
这可以在函数的实现中轻松完成,或者在任何范围之外的 .cpp 文件主体中完成。
我想做的是将这样的数组作为我正在使用的类的成员,如下所示:
class DataProvider : public SomethingElse
{
const char* const mStringData[] = {"Name1", "Name2", "Name3", ... "NameX"};
public:
DataProvider();
~DataProvider();
char* GetData()
{
int index = GetCurrentIndex(); //work out the index based on some other data
return mStringData[index]; //error checking and what have you omitted
}
};
但是,编译器会抱怨,我似乎无法弄清楚为什么。 是否可以在类定义中一步声明并初始化这样的数组? 有更好的替代方案吗?
This is related to some other questions, such as: this, and some of my other questions.
In this question, and others, we see we can declare and initialise string arrays in one nice step, for example:
const char* const list[] = {"zip", "zam", "bam"}; //from other question
This can be done in the implementation of a function with no bother, or in the body of a .cpp file, outside any scope.
What I want to do is to have an array like this as as member of a class I am using, something like this:
class DataProvider : public SomethingElse
{
const char* const mStringData[] = {"Name1", "Name2", "Name3", ... "NameX"};
public:
DataProvider();
~DataProvider();
char* GetData()
{
int index = GetCurrentIndex(); //work out the index based on some other data
return mStringData[index]; //error checking and what have you omitted
}
};
But, the compiler complains and I can't seem to work out why. Is it possible to declare and initialise an array like this in one step in a class definition? Are there alternatives that are better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用关键字 static 和外部初始化使数组成为类的静态成员:
在头文件中:
在 .cpp 文件中:
Use the keyword static and external initialization to make the array a static member of the class:
In the header file:
In the .cpp file:
不能像这样声明数组 (const char* []) 的原因是:
const char* []
不会说明编译器需要为每个实例分配多少空间(您的数组被声明为实例变量)。此外,您可能希望将该数组设为静态,因为它本质上是一个常量值。
The reason you can't declare your array like that (const char* []) is that:
const char* []
does not state how much space the compiler needs to allocate for each instance (your array is declared as instance variable).Besides, you probably want to make that array static, since it is in essence a constant value.
这在 C++ 中是不可能的。 您不能直接初始化数组。 相反,您必须为其指定大小(在您的情况下为 4),并且必须在 DataProvider 的构造函数中初始化数组:
请注意,您必须放弃数组中指针的常量性,因为你不能直接初始化数组。 但是您需要稍后将指针设置为正确的值,因此指针需要是可修改的。
如果数组中的值仍然是 const,则唯一的方法是使用静态数组:
拥有静态数组意味着所有对象都将共享该数组。 这样你也节省了内存。
This is not possible in C++. You cannot directly initialize the array. Instead you have to give it the size it will have (4 in your case), and you have to initialize the array in the constructor of DataProvider:
Note that you have to give up on the const-ness of the pointers in the array, since you cannot directly initialize the array. But you need to later set the pointers to the right values, and thus the pointers need to be modifiable.
If your values in the array are const nevertheless, the only way is to use a static array:
Having the static array means all objects will share that array. Thus you will have saved memory too.