1

If I have class A that declares Class B and instantiates an array of it.

class A{
    class B{
        int x, y;
    };
    B arrB[10];

 public:
    A();
};

Then, my issue is that I would like to initialize the first two objects of "arrB" in Class A using initialization list:

A::A(): arrB[0](1, 2), arrB[1](3, 4) {}

But the compiler does not accept it.

Can I initialize specific objects of the array or not? If yes, how to do it?

Thanks

6
  • Is there a reason you need this in the initialization list instead of just in the constructor function body? Commented Jul 11, 2019 at 19:24
  • Maybe something like: arrB{{1,2},{3,4},{},{},{},{},{},{},{},{}} or arrB{{1,2},{3,4}} however rest of the elements gets random values and it must be a struct or public x,y Commented Jul 11, 2019 at 19:29
  • @KIIV: The second form is fine; the rest of the elements will be zero initialized Commented Jul 11, 2019 at 19:34
  • The compiler complains on the second form as it gives error asking about init of the rest of the array items Commented Jul 11, 2019 at 19:38
  • After the edits in question it won't work. It'll work with struct { int x, y; }; Commented Jul 11, 2019 at 19:45

2 Answers 2

2

The problem is that B hides its members by default as private because it is a class. Declare B a struct, or expose int x, y as public to be able to use aggregate initialization:

class A{
    class B{
        public:
        int x, y;
    };
    B arrB[10] = {{1,2}};

 public:
    A();
};

The second problem is that you're not using aggregate initialization properly.

A::A(): arrB[0](1, 2), arrB[1](3, 4) {}

Should be

A::A(): arrB{{1, 2}, {3, 4}} {}

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

In order to initialize an array in the class member initialization list you need to use curly braces like you would if you were initializing an array in a function.

Thus if you wanted to initialize the first and second element of the array you would need to use at least

A() : arrB{{1,2}, {3,4}} {}

as the first set ({1,2}) says make arrB[0] a B with x and y initialized to 1 and 2 respectively. The second set ({3,4}) says make arrB[1] a B with x and y initialized to 3 and 4 respectively.

You do have to do one thing though in order to make this work. You either need to make B and aggregate by making x and y public, or you can provide a constructor for B that takes to values. Doing that lets you have either

class A{
    class B{
    public:
        int x, y;
    };
    B arrB[10];

 public:
    A() : arrB{{}, {3,4}} {}
};

int main() {
    A a;
}

or

class A{
    class B{
        int x, y;
    public:
        B() : x(), y() {} // need this so default instances are value initialized (zeroed in this case)
        B(int x, int y) : x(x), y(y) {}
    };
    B arrB[10];

 public:
    A() : arrB{{}, {3,4}} {}
};

int main() {
    A a;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.