Initialise array of struct using C syntax

S

Simon Elliott

I have some legacy code which initialises an array using C syntax like
this:

struct bar
{
int i1_;
int i2_;
bool b1_;
};

int main(int argc, char **argv)
{
bar myBar[] =
{
{1,2,false},
{2,2,true},
};
return 0;
}

I now want to use struct bar in C++ and give it a constructor or two:

struct bar
{
bar(void):i1_(0),i2_(0),b1_(false){}
bar(int i1, int i2, bool b1):i1_(i1),i2_(i2),b1_(b1){}
int i1_;
int i2_;
bool b1_;
};

Is there any way I can still use the above syntax for initialising an
array of bar?
 
M

Markus Moll

Hi

Simon Elliott said:
struct bar
{
bar(void):i1_(0),i2_(0),b1_(false){}
bar(int i1, int i2, bool b1):i1_(i1),i2_(i2),b1_(b1){}
int i1_;
int i2_;
bool b1_;
};

Is there any way I can still use the above syntax for initialising an
array of bar?

No, but what about:

bar array[] = {
bar(1,2,true),
bar(3,4,false),
bar(5,0,true),
bar()
};

Markus
 
S

Simon Elliott

Simon Elliott said:
struct bar
{
bar(void):i1_(0),i2_(0),b1_(false){}
bar(int i1, int i2, bool b1):i1_(i1),i2_(i2),b1_(b1){}
int i1_;
int i2_;
bool b1_;
};

Is there any way I can still use the above syntax for initialising
an array of bar?

No, but what about:

bar array[] = {
bar(1,2,true),
bar(3,4,false),
bar(5,0,true),
bar()
};

Thanks, that's exactly what I was looking for.
 

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
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top