constructor initialization list, array of objects

Discussion in 'C++' started by j j, Jun 17, 2012.

  1. j j

    j j Guest

    Hi

    How can I initialize an array of objects in a constructor initialization
    list with a specific values. The following does not work.

    #include <iostream>

    using namespace std;

    struct Wheel {
    int radius;

    Wheel (int radius) : radius (radius) {}
    };

    struct Car {
    Wheel wheels[2];

    Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
    Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
    };

    int main (void)
    {
    Car c1(1, 2);
    Car c2;
    return 0;
    }
    j j, Jun 17, 2012
    #1
    1. Advertising

  2. j j <> writes:

    > How can I initialize an array of objects in a constructor initialization
    > list with a specific values. The following does not work.
    >
    > #include <iostream>
    > using namespace std;
    > struct Wheel {
    > int radius;
    > Wheel (int radius) : radius (radius) {}
    > };
    > struct Car {
    > Wheel wheels[2];
    > Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
    > Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
    > };


    Does compile with gcc-4.6.3, requires -std=c++0x though to suppress
    warnings (which, btw, are perfectly informative).

    -- Alain.
    Alain Ketterlin, Jun 17, 2012
    #2
    1. Advertising

  3. On 17.06.2012 12:24, j j wrote:
    > Hi
    >
    > How can I initialize an array of objects in a constructor initialization
    > list with a specific values. The following does not work.
    >
    > #include<iostream>
    >
    > using namespace std;
    >
    > struct Wheel {
    > int radius;
    >
    > Wheel (int radius) : radius (radius) {}
    > };
    >
    > struct Car {
    > Wheel wheels[2];
    >
    > Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
    > Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
    > };
    >
    > int main (void)
    > {
    > Car c1(1, 2);
    > Car c2;
    > return 0;
    > }


    With a compiler that supports C++11 brace initializers the above
    compiles directly.

    With Visual C++ you can instead do a bit of hybrid C++03 / C++11:


    <code>
    #include <iostream>
    #include <array>
    using namespace std;

    struct Wheel {
    int radius;
    Wheel (int radius) : radius (radius) {}
    };

    struct Car {
    static array<Wheel, 2> values( Wheel const& w1, Wheel const& w2 )
    {
    array<Wheel, 2> const result = { w1, w2 };
    return result;
    }

    array<Wheel, 2> wheels;

    Car () : wheels( values( 5, 6 ) ) {}
    Car (int r1, int r2) : wheels( values( r1, r2 ) ) {}
    };

    int main()
    {
    Car c1(1, 2);
    Car c2;
    }
    <code>


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Jun 17, 2012
    #3
  4. j j

    j j Guest

    W dniu niedziela, 17 czerwca 2012 14:53:26 UTC+2 użytkownik Alf P. Steinbach napisał:
    > On 17.06.2012 12:24, j j wrote:
    > > Hi
    > >
    > > How can I initialize an array of objects in a constructor initialization
    > > list with a specific values. The following does not work.
    > >
    > > #include<iostream>
    > >
    > > using namespace std;
    > >
    > > struct Wheel {
    > > int radius;
    > >
    > > Wheel (int radius) : radius (radius) {}
    > > };
    > >
    > > struct Car {
    > > Wheel wheels[2];
    > >
    > > Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
    > > Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
    > > };
    > >
    > > int main (void)
    > > {
    > > Car c1(1, 2);
    > > Car c2;
    > > return 0;
    > > }

    >
    > With a compiler that supports C++11 brace initializers the above
    > compiles directly.


    In fact, the brace initializers are supported by C++ 11 but the above example
    does not compile using g++ --std=c++0x (g++ version 4.4.5).
    j j, Jun 23, 2012
    #4
  5. On 23.06.2012 13:00, j j wrote:
    > W dniu niedziela, 17 czerwca 2012 14:53:26 UTC+2 użytkownik Alf P. Steinbach napisał:
    >> On 17.06.2012 12:24, j j wrote:
    >>> Hi
    >>>
    >>> How can I initialize an array of objects in a constructor initialization
    >>> list with a specific values. The following does not work.
    >>>
    >>> #include<iostream>
    >>>
    >>> using namespace std;
    >>>
    >>> struct Wheel {
    >>> int radius;
    >>>
    >>> Wheel (int radius) : radius (radius) {}
    >>> };
    >>>
    >>> struct Car {
    >>> Wheel wheels[2];
    >>>
    >>> Car () : wheels {Wheel(5),Wheel(6)} {} // does not compile
    >>> Car (int r1, int r2) : wheels ({r1,r2}) {} // does not compile
    >>> };
    >>>
    >>> int main (void)
    >>> {
    >>> Car c1(1, 2);
    >>> Car c2;
    >>> return 0;
    >>> }

    >>
    >> With a compiler that supports C++11 brace initializers the above
    >> compiles directly.

    >
    > In fact, the brace initializers are supported by C++ 11 but the above example
    > does not compile using g++ --std=c++0x (g++ version 4.4.5).


    First, brace initializers are not "supported" by C++11, they (in the
    general form of C++11) are defined by C++11.

    Secondly, the code compiles fine with g++ 4.6.1, from which you can
    conclude that either you've done something wrong (most likely), or C++11
    brace initializers are not supported by your earlier version.

    Summing up, that was a pretty dumb comment. Instead of trying to assert
    dubious "in fact"'s, just *ask* when you're confused. Also try posting
    with your real name. Posting anonymously you are signaling that you're
    an adolescent or pre-teen who is not too serious.

    Cheers & hth.

    - Alf
    Alf P. Steinbach, Jun 23, 2012
    #5
    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. Dave Theese
    Replies:
    8
    Views:
    405
  2. toton
    Replies:
    5
    Views:
    920
    Victor Bazarov
    Sep 28, 2006
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,202
  4. aaragon
    Replies:
    2
    Views:
    606
    James Kanze
    Nov 2, 2008
  5. balaji
    Replies:
    3
    Views:
    925
    Jens Thoms Toerring
    Aug 10, 2011
Loading...

Share This Page