0

I'm trying to initialize a std::array of objects within the constructor of another class. It seems like aggregate initialization should work here, but I can't figure out the appropriate syntax. How do I go about doing this?

class A {
        const int a;
public:
        A(int an_int) : a(an_int) {}
};

class B {
        std::array<A,3> stuff;
public:
        B() :
        stuff({1,2,3}) // << How do I do this?
        {}
};

int main() {
        B b;
        return 0;
}

1 Answer 1

4

You just need an extra pair of braces:

B() : stuff({{1,2,3}}) {}
            ^       ^

Or you can replace parentheses with braces:

B() : stuff {{1,2,3}} {}
            ^       ^
Sign up to request clarification or add additional context in comments.

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.