简体   繁体   English

如何在类构造函数中初始化数组?

[英]How to initialize an array in a class constructor?

Working in Xcode on Mac OS X Leopard in C++: 使用C ++在Mac OS X Leopard上使用Xcode

I have the following code: 我有以下代码:

class Foo{

private:
    string bars[];

public:
    Foo(string initial_bars[]){
        bars = initial_bars;
    }
}

It does not compile and throws the following error: 它不编译并抛出以下错误:

error: incompatible types in assignment of 'std::string*' to 'std::string [0u]'

I notice that removing the line bars = initial_bars; 我注意到删除了line bars = initial_bars; solves the problem. 解决了这个问题。 It seems like I am not doing the assignment correctly. 好像我没有正确地完成任务。 How could I go about fixing this problem? 我怎么能解决这个问题呢?

EDIT: 编辑:

The variable bars is an array of strings. 变量条是一个字符串数组。 In the main function I initialize it like this: 在main函数中,我将它初始化为:

string bars[] = {"bar1", "bar2", "bar3"};

But it can contain an arbitrary number of members. 但它可以包含任意数量的成员。

Arrays behave like const pointers, you can't assign pointers to them. 数组的行为类似于const指针,您无法为它们指定指针。 You also can't directly assign arrays to each other. 您也无法直接为彼此分配数组。

You either 你要么

  • use a pointer member variable 使用指针成员变量
  • have a fixed size of bar s you get and initialize your member array with its contents 你得到一个固定大小的bar并用你的内容初始化你的成员数组
  • just use a reference to a std container like std::vector 只需使用std容器的引用,如std::vector

It is possible to "value-initialize" array members as follows: 可以按如下方式“值初始化”数组成员:

class A {
public:
  A () 
  : m_array ()       // Initializes all members to '0' in this case
  {
  }

private:
  int m_array[10];
};

For POD types this is important as if you don't list 'm_array' in the member initialization list then the array elements will have indeterminate values. 对于POD类型,这很重要,就好像您没有在成员初始化列表中列出'm_array'那样数组元素将具有不确定的值。

In general it's better to initialize members in the member-initialization-list, otherwise the members will be initialized twice: 通常,最好在member-initialization-list中初始化成员,否则成员将初始化两次:

A (std::vector<int> const & v)
// : m_v ()   // 'm_v' is implicitly initialized here
{
  m_v = v;    // 'm_v' now assigned to again
}

More efficiently written as: 更有效地写作:

A (std::vector<int> const & v)
: m_v (v)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用构造函数 C++ 初始化数组 class - How to initialize an array class with constructor C++ 在类中声明数组并在构造函数中对其进行初始化 - Declare Array in Class and Initialize it in Constructor 如何在类构造函数中初始化动态分配的数组 - How do I initialize a dynamically allocated array within a class constructor "如何在没有默认构造函数的情况下初始化类的 std::array?" - How can I initialize an std::array of a class without a default constructor? 如何在运行时使用构造函数初始化在类中私有的数组? - How to initialize an array that is private in a class using constructor at runtime? 如何在 c++ 的 class 构造函数中将整个数组初始化为单个元素 - How to initialize whole array to single element in class constructor in c++ 在另一个类的构造函数中初始化类对象的数组 - Initialize array of class objects in constructor of another class 如何在类的构造函数中初始化向量 - How to initialize a vector in the constructor of a class 如何在构造函数中初始化对象数组? - How to initialize array of objects in constructor? 如何在构造函数中轻松地初始化数组? - How to initialize an array easily in constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM