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

Joe*_*man 6 c++ arrays data-structures

如果我有课:

class A
{
private:
     char z;
     int x;

public:
     A(char inputz, int inputx);
     ~A() {}
}
Run Code Online (Sandbox Code Playgroud)

我想A在课堂上制作一个数组B.

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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*k B 3

默认构造函数将被自动调用(对于非 POD)。如果您需要不同的构造函数,那么您就不走运了,但是您可以使用vector它来支持您所需要的。