Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;头球?_C++_Arrays_Header_Initialization_Constants - Fatal编程技术网

C++ 如何在c++;头球?

C++ 如何在c++;头球?,c++,arrays,header,initialization,constants,C++,Arrays,Header,Initialization,Constants,这与其他一些问题有关,如:,以及我的其他一些问题 在、和其他中,我们可以在一个很好的步骤中声明和初始化字符串数组,例如: const char* const list[] = {"zip", "zam", "bam"}; //from other question 这可以在无需麻烦的函数实现中完成,也可以在任何范围之外的.cpp文件体中完成 我想做的是将这样一个数组作为我正在使用的类的成员,如下所示: class DataProvider : public SomethingElse {

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

在、和其他中,我们可以在一个很好的步骤中声明和初始化字符串数组,例如:

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
    }

};

但是,编译器抱怨,我似乎不知道为什么。是否可以在类定义中一步声明和初始化这样的数组?有更好的选择吗?

< P>这在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);
    }
};
请注意,您必须放弃数组中指针的常量,因为您无法直接初始化数组。但是以后需要将指针设置为正确的值,因此指针需要可修改

如果数组中的值是常量,则唯一的方法是使用静态数组:

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

拥有静态数组意味着所有对象都将共享该数组。因此,您也将节省内存。

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

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

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

使用关键字static和external initialization使该数组成为类的静态成员:

在头文件中:

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

您需要在标头声明中为数组指定一个大小。是否确定?它对我来说很好(Visual C++ 2005),我以前已经使用过几次。除非它是标准中未定义的行为(我现在不查了),否则我相信它会起作用。您在使用初始化器时很好。编译器将根据初始化器中的项数计算大小。是的,我认为省略大小是有效的,那么数组就不完整了。但在标题中,您将不知道数组的大小。因此也不可能使用sizeof。--因为OP使用的是(成员)函数GetCurrentIndex(),所以如果在同一个.cpp文件中定义了sizeof运算符,它就可以使用它。“impimation”实际上应该拼写为“implementation”,即
const char*[]
确实说明了编译器需要为每个实例分配多少空间-只是一个指向内存的指针。这就是为什么非静态变量不允许使用静态数组声明的真正原因;每个新实例都需要额外的内存分配,而这种处理开销传统上是在构造函数中显式处理的。“拥有静态数组意味着所有对象都将共享该数组”,这也意味着您可能刚刚损害了面向对象编程的基本原则。