Question

My question is that: for example I have two classes x and y

class X{
public:
      X(int, int, string);

private: int a;
         int b;
         string c;
};

class Y{

private: X x[10];
}

My question is that how to initialize this array of 10 x objects through constructor of Y? Initializer list? How to initialize these 10 objects of x in y.

Was it helpful?

Solution

In C++03 you cannot do this.

In C++11, like this:

class Y
{
private:
    X x[3];
public:
    Y()
    : x { X(1, 2, "a"), X(2, 3, "b"), X(3, 4, "c") }
    {
    }
};

Alternatively you can say:

    : x { {1, 2, "a"}, {2, 3, "b"}, {3, 4, "c"} }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top