Initializing Non-default-constructible Elements in a Member Array

B

better_cs_now

Hello all,

In this code:

class Bar {
public:
Bar(int) {} // ***Not default constructible***
};

class Foo {
public:
Foo() : m_Bar({Bar(1), Bar(2)}) {} // Not syntactically correct!
private:
Bar m_Bar[2];
};

What is the proper way to initialize m_Bar? Thanks to all!

Best,
Dave
 
V

Victor Bazarov

In this code:

class Bar {
public:
Bar(int) {} // ***Not default constructible***
};

class Foo {
public:
Foo() : m_Bar({Bar(1), Bar(2)}) {} // Not syntactically correct!
private:
Bar m_Bar[2];
};

What is the proper way to initialize m_Bar? Thanks to all!

There is no way, proper or otherwise. Use a a standard container
(vector) which can be copy-initialised from a static Bar array.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top