array initialization in initialization list.

Discussion in 'C++' started by toton, Sep 28, 2006.

  1. toton

    toton Guest

    I can initialize an array of class with a specific class as,
    class Test{
    Test x[2] = {Test(3),Test(6)}; using array initialization list. (Note
    Test do NOT have a default ctor).
    Is it possible to do so in the class parameter initialization using
    specific ctor?
    class TestContainer{
    Test _x;
    TestContainer(int size) : _x(Test(size) {}
    This works.
    class TestContainer{
    Test _x[2];
    TestContainer(int size) : ??? {} //how to do it like array
    In actual case Test class is a specific kind of semi-fixed-size
    container (not defined in STL! ) which needs a size parameter. The
    class like TestContainer holds such Test class two instance (and only
    two). I know that an alternative is to store Test _x1, Test _x2; and in
    initialization list TestContainer(int size) : _x1(Test(size) ,
    _x2(Test(size)){} , or in general case, using an STL vector with
    reserve space 2. Or storing a pointer instead of the object itself.
    But my question is, In object initializer list can a array
    initialization be called? If yes, what is the syntax?
    toton, Sep 28, 2006
    1. Advertisements

  2. No.

    Victor Bazarov, Sep 28, 2006
    1. Advertisements

  3. toton

    David Harmon Guest

    On 28 Sep 2006 03:50:03 -0700 in comp.lang.c++, "toton"
    Should be
    TestContainer(int size) : _x(size) {}

    Pay no attention to whether or not it resembles array initialization
    syntax at this point.

    See also "[10.6] Should my constructors use "initialization lists"
    or "assignment"?" in Marshall Cline's C++ FAQ. You can get the FAQ
    David Harmon, Sep 28, 2006
  4. toton

    mlimber Guest

    You can't with arrays, but you can with std::vector (which you should
    probably be using anyway, For

    #include <vector>
    using namespace std;

    template<typename T>
    class Initializer
    vector<T> v_;
    Initializer( const unsigned capacity=0 )
    v_.reserve( capacity );

    Initializer& Add( const T& t )
    return *this;

    operator vector<T>() const
    return v_;

    class Example
    const vector<double> v_;
    Example( double d0, double d1, double d2 )
    : v_( Initializer<double>( 3 )
    .Add(d2) )
    // ...

    Cheers! --M
    mlimber, Sep 28, 2006
  5. toton

    Gavin Deane Guest

    Annoyingly, IIRC, arrays are the one thing that cannot be initialised
    in the initialiser list as you would like to.

    Gavin Deane
    Gavin Deane, Sep 28, 2006
  6. No, structs are also that thing. Aggregates cannot be initialised
    except with default values. There is a proposal on the table, IIRC,
    that might change that.

    Victor Bazarov, Sep 28, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.