如何在 c++ 中声明数组 标头?

发布于 2024-07-08 03:10:57 字数 948 浏览 9 评论 0原文

这与其他一些问题有关,例如:这个,以及我的一些其他问题。

这个问题和其他问题中,我们看到我们可以通过一个不错的步骤来声明和初始化字符串数组,例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦爱 2024-07-15 03:10:57

使用关键字 static 和外部初始化使数组成为类的静态成员:

在头文件中:

class DataProvider : public SomethingElse
{
    static const char* const mStringData[];

public:
    DataProvider();
    ~DataProvider();

    const char* const GetData()
    {
        int index = GetCurrentIndex(); //work out the index based on some other data
        return mStringData[index]; //error checking and what have you omitted
    }

};

在 .cpp 文件中:

const char* const DataProvider::mStringData[] = {"Name1", "Name2", "Name3", ... "NameX"};

Use the keyword static and external initialization to make the array a static member of the class:

In the header file:

class DataProvider : public SomethingElse
{
    static const char* const mStringData[];

public:
    DataProvider();
    ~DataProvider();

    const char* const GetData()
    {
        int index = GetCurrentIndex(); //work out the index based on some other data
        return mStringData[index]; //error checking and what have you omitted
    }

};

In the .cpp file:

const char* const DataProvider::mStringData[] = {"Name1", "Name2", "Name3", ... "NameX"};
ㄖ落Θ余辉 2024-07-15 03:10:57

不能像这样声明数组 (const char* []) 的原因是:

  • 类声明中不能有初始值设定项,因此
  • 语法 const char* [] 不会说明编译器需要为每个实例分配多少空间(您的数组被声明为实例变量)。

此外,您可能希望将该数组设为静态,因为它本质上是一个常量值。

The reason you can't declare your array like that (const char* []) is that:

  • you can't have initializers in the class declaration, and so
  • the syntax 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.

梦罢 2024-07-15 03:10:57

这在 C++ 中是不可能的。 您不能直接初始化数组。 相反,您必须为其指定大小(在您的情况下为 4),并且必须在 DataProvider 的构造函数中初始化数组:

class DataProvider {
    enum { SIZEOF_VALUES = 4 };
    const char * values[SIZEOF_VALUES];

    public:
    DataProvider() {
        const char * const v[SIZEOF_VALUES] = { 
            "one", "two", "three", "four" 
        };
        std::copy(v, v + SIZEOF_VALUES, values);
    }
};

请注意,您必须放弃数组中指针的常量性,因为你不能直接初始化数组。 但是您需要稍后将指针设置为正确的值,因此指针需要是可修改的。

如果数组中的值仍然是 const,则唯一的方法是使用静态数组:

/* in the header file */
class DataProvider {
    enum { SIZEOF_VALUES = 4 };
    static const char * const values[SIZEOF_VALUES];
};

/* in cpp file: */

const char * const DataProvider::values[SIZEOF_VALUES] = 
    { "one", "two", "three", "four" };

拥有静态数组意味着所有对象都将共享该数组。 这样你也节省了内存。

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:

class DataProvider {
    enum { SIZEOF_VALUES = 4 };
    const char * values[SIZEOF_VALUES];

    public:
    DataProvider() {
        const char * const v[SIZEOF_VALUES] = { 
            "one", "two", "three", "four" 
        };
        std::copy(v, v + SIZEOF_VALUES, values);
    }
};

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:

/* in the header file */
class DataProvider {
    enum { SIZEOF_VALUES = 4 };
    static const char * const values[SIZEOF_VALUES];
};

/* in cpp file: */

const char * const DataProvider::values[SIZEOF_VALUES] = 
    { "one", "two", "three", "four" };

Having the static array means all objects will share that array. Thus you will have saved memory too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文