How do you declare arrays in a c++ header?

后端 未结 3 1056
天涯浪人
天涯浪人 2021-02-04 01:18

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

3条回答
  •  一整个雨季
    2021-02-04 01:42

    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"};
    

提交回复
热议问题