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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top