By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Initialise array of struct using C syntax

P: n/a
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?

--
Simon Elliott http://www.ctsn.co.uk
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi

"Simon Elliott" <Simon at ctsn.co.uk> wrote:
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

Jul 23 '05 #2

P: n/a
On 13/04/2005, Markus Moll wrote:
"Simon Elliott" <Simon at ctsn.co.uk> wrote:
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.
--
Simon Elliott http://www.ctsn.co.uk
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.