Initialise array of struct using C syntax

Discussion in 'C++' started by Simon Elliott, Apr 13, 2005.

  1. 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
    Simon Elliott, Apr 13, 2005
    #1
    1. Advertising

  2. Simon Elliott

    Markus Moll Guest

    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
    Markus Moll, Apr 13, 2005
    #2
    1. Advertising

  3. 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
    Simon Elliott, Apr 13, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stuart Norris
    Replies:
    5
    Views:
    500
    Dave Thompson
    Sep 29, 2003
  2. Eric Sosman
    Replies:
    0
    Views:
    570
    Eric Sosman
    Mar 18, 2012
  3. Arne Vajhøj
    Replies:
    4
    Views:
    369
    Arne Vajhøj
    Mar 21, 2012
  4. Tuan  Bui
    Replies:
    14
    Views:
    462
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  5. Joost Kraaijeveld
    Replies:
    2
    Views:
    1,365
Loading...

Share This Page