在另一个类的构造函数中初始化类对象的数组

时间:2022-10-11 21:26:05

If I have a class:

如果我有课:

class A
{
private:
     char z;
     int x;

public:
     A(char inputz, int inputx);
     ~A() {}
}

I want to make an array of A in class B.

我想在B类中创建一个A数组。

class B
{
private:
    A arrayofa[26];

public:
    B();
    ~B() {}
    void updatearray(); // This will fill the array with what is needed.
}


class B
{
    B:B()
    {
        updatearray();
        std::sort( &arrayofa[0], &arrayofa[26], A::descend );
    }
}

How do I explicitly initialize arrayofa in the constructor of B?

如何在B的构造函数中显式初始化arrayofa?

3 个解决方案

#1


3  

The default constructor will be called automatically (for non-POD). If you need a different constructor you're out of luck, but you can use vector instead which will support what you need.

将自动调用默认构造函数(对于非POD)。如果你需要一个不同的构造函数你运气不好,但你可以使用vector来支持你需要的东西。

#2


3  

You can't.

Each element in the array will be initialized by the default constructor (no parameter constructor).

数组中的每个元素都将由默认构造函数(无参数构造函数)初始化。

The best alternative is to use a vector.
Here you specify an a value that will be copy constructed into all members of the vector:

最好的选择是使用矢量。在这里,您指定一个值,该值将被复制到向量的所有成员中:

class B
{
private:
     std::vector<A> arrayofa;
public:
     B()
        : arrayofa(26, A(5,5))
          // create a vector of 26 elements.
          // Each element will be initialized using the copy constructor
          // and the value A(5,5) will be passed as the parameter.
     {}
     ~B(){}
     void updatearray();//This will fill the array with what is needed
}

#3


1  

First there should be a default constructor for class A, else B() will not compile. It will try to call the default constructor of members of class B before the body of the constructor starts executing.

首先应该有一个A类的默认构造函数,否则B()将无法编译。它将尝试在构造函数体开始执行之前调用类B成员的默认构造函数。

You can initialize arrayofa like this:

你可以像这样初始化arrayofa:

void B::updatearray()
{
    arrayofa[0] = A('A', 10);
    arrayofa[1] = A('B', 20);
    ...
}

It would be better to use std::vector instead of array.

使用std :: vector而不是数组会更好。

std::vector<A> v(26, A('a', 10)); //initialize all 26 objects with 'a' and 10

#1


3  

The default constructor will be called automatically (for non-POD). If you need a different constructor you're out of luck, but you can use vector instead which will support what you need.

将自动调用默认构造函数(对于非POD)。如果你需要一个不同的构造函数你运气不好,但你可以使用vector来支持你需要的东西。

#2


3  

You can't.

Each element in the array will be initialized by the default constructor (no parameter constructor).

数组中的每个元素都将由默认构造函数(无参数构造函数)初始化。

The best alternative is to use a vector.
Here you specify an a value that will be copy constructed into all members of the vector:

最好的选择是使用矢量。在这里,您指定一个值,该值将被复制到向量的所有成员中:

class B
{
private:
     std::vector<A> arrayofa;
public:
     B()
        : arrayofa(26, A(5,5))
          // create a vector of 26 elements.
          // Each element will be initialized using the copy constructor
          // and the value A(5,5) will be passed as the parameter.
     {}
     ~B(){}
     void updatearray();//This will fill the array with what is needed
}

#3


1  

First there should be a default constructor for class A, else B() will not compile. It will try to call the default constructor of members of class B before the body of the constructor starts executing.

首先应该有一个A类的默认构造函数,否则B()将无法编译。它将尝试在构造函数体开始执行之前调用类B成员的默认构造函数。

You can initialize arrayofa like this:

你可以像这样初始化arrayofa:

void B::updatearray()
{
    arrayofa[0] = A('A', 10);
    arrayofa[1] = A('B', 20);
    ...
}

It would be better to use std::vector instead of array.

使用std :: vector而不是数组会更好。

std::vector<A> v(26, A('a', 10)); //initialize all 26 objects with 'a' and 10