Stylistic question -- initialization lists and vectors

Discussion in 'C++' started by Denis Remezov, Apr 30, 2004.

  1. Russell Silva wrote:
    >

    [snip]
    > The problem is in B's constructor. I want to create a vector<foo>
    > with multiple, different elements to pass to A's constructor during
    > initialization. However the vector constructors do not allow you to
    > initialize its elements to separate values (you can only "flood fill"
    > the vector with one element).

    [snip]

    How about these two constructors (especially the first one, the second
    one is only needed as a copy constructor):

    template <typename InputIterator>
    vector(InputIterator first, InputIterator last,
    const Allocator& = Allocator());

    vector(const vector& x);

    I'd think they would do what you need.

    Denis
     
    Denis Remezov, Apr 30, 2004
    #1
    1. Advertising

  2. I have a class A with a member variable type vector<foo> which is
    initialized upon construction (no standard () constructor):

    class A {
    protected:
    vector<foo> foo_vector;
    public:
    A(const vector<foo>& init_foo_vector);
    }

    A::A(const vector<foo>& init_foo_vector) : foo_vector(init_foo_vector)
    {}

    I have a class B which has a member variable of class A:

    class B {
    protected:
    A a;
    public
    B();
    }

    The problem is in B's constructor. I want to create a vector<foo>
    with multiple, different elements to pass to A's constructor during
    initialization. However the vector constructors do not allow you to
    initialize its elements to separate values (you can only "flood fill"
    the vector with one element). I'm trying to follow resource
    acquisition is initialization, which is why I'm shunning null
    constructors and passing dummy values.
    So what's the best way to construct a vector, in the initialization
    list, with multiple, different elements? I'm thinking I could make a
    static class function in B that created the necessary vector, but I
    don't know if there are more elegant solutions or if there is some way
    of avoiding this problem entirely. It's a stylistic question; clearly
    the static function would suffice as far as functionality is
    concerned.

    Thanks in advance,

    Russell Silva
     
    Russell Silva, Apr 30, 2004
    #2
    1. Advertising

  3. "Russell Silva" <> wrote in message
    news:...
    ....
    > A::A(const vector<foo>& init_foo_vector)

    ....
    > class B {
    > protected:
    > A a;
    > public
    > B();
    > }
    >
    > The problem is in B's constructor. I want to create a vector<foo>
    > with multiple, different elements to pass to A's constructor during
    > initialization. However the vector constructors do not allow you to
    > initialize its elements to separate values (you can only "flood fill"
    > the vector with one element).


    If the elements are constant, it is often possible to use a global
    array to initialize the contents of a vector.
    For example:
    foo const a[2] = { 5, 3 };

    B::B()
    : A( std::vector<foo>( a, a+2 ) )
    { }

    But this style isn't necessarily optimum.

    > I'm trying to follow resource
    > acquisition is initialization, which is why I'm shunning null
    > constructors and passing dummy values.


    Be careful not to be dogmatic in your application of RAII.
    Direct initialization should indeed be preferred in most cases,
    because it helps avoid bugs, enables a better usage of const,
    and can improve performance in some situations.
    But providing a default constructor, and allowing a default
    (sometimes called resource-free) state in your classes is also
    useful in many circumstances.
    In the end, it is a design choice, based on what invariants you
    want your class to guarantee. I.e.: Should every instance always
    be associated with a valid resource?). If not, two-step
    initialization is perfectly ok.

    > So what's the best way to construct a vector, in the initialization
    > list, with multiple, different elements? I'm thinking I could make a
    > static class function in B that created the necessary vector, but I
    > don't know if there are more elegant solutions or if there is some way
    > of avoiding this problem entirely. It's a stylistic question; clearly
    > the static function would suffice as far as functionality is
    > concerned.


    The static function is the only solution that can always work
    in this circumstance (other than the array-based approach above).

    But nothing in the sample you posted justifies not allowing
    instances of class A to have a default-constructed, empty state.


    Regards,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Apr 30, 2004
    #3
  4. Denis Remezov

    Dave Moore Guest

    Denis Remezov <> wrote in message news:<>...
    > Russell Silva wrote:
    > >

    > [snip]
    > > The problem is in B's constructor. I want to create a vector<foo>
    > > with multiple, different elements to pass to A's constructor during
    > > initialization. However the vector constructors do not allow you to
    > > initialize its elements to separate values (you can only "flood fill"
    > > the vector with one element).

    > [snip]
    >
    > How about these two constructors (especially the first one, the second
    > one is only needed as a copy constructor):
    >
    > template <typename InputIterator>
    > vector(InputIterator first, InputIterator last,
    > const Allocator& = Allocator());
    >
    > vector(const vector& x);
    >
    > I'd think they would do what you need.
    >
    > Denis


    You could also create a (private) static std::vector<foo> member of
    class B and use that to do the initialization ... that is, assuming
    that you always want to initialize with the same values. This
    approach has the advantage that it hides the gory details (i.e. that B
    needs an initialization vector) from users of class B, thereby
    simplifying the interface somewhat.

    HTH, Dave Moore
     
    Dave Moore, Apr 30, 2004
    #4
  5. Denis Remezov

    Siemel Naran Guest

    "Ivan Vecerina" <> wrote in message
    > "Russell Silva" <> wrote in message


    > > I'm trying to follow resource
    > > acquisition is initialization, which is why I'm shunning null
    > > constructors and passing dummy values.

    >
    > Be careful not to be dogmatic in your application of RAII.
    > Direct initialization should indeed be preferred in most cases,
    > because it helps avoid bugs, enables a better usage of const,
    > and can improve performance in some situations.


    Good point, although I almost always use RAII. For a compromise between the
    2 methods you can haev a function that returns a constructed object. It
    would typically create the object with a call to the default constructor,
    then call the object's set functions, which could even be virtual (can't
    call virtual functions in the constructor). We could even make the
    constructor private or protected.

    > But providing a default constructor, and allowing a default
    > (sometimes called resource-free) state in your classes is also
    > useful in many circumstances.
    > In the end, it is a design choice, based on what invariants you
    > want your class to guarantee. I.e.: Should every instance always
    > be associated with a valid resource?). If not, two-step
    > initialization is perfectly ok.


    > > So what's the best way to construct a vector, in the initialization
    > > list, with multiple, different elements? I'm thinking I could make a
    > > static class function in B that created the necessary vector, but I
    > > don't know if there are more elegant solutions or if there is some way
    > > of avoiding this problem entirely. It's a stylistic question; clearly
    > > the static function would suffice as far as functionality is
    > > concerned.

    >
    > The static function is the only solution that can always work
    > in this circumstance (other than the array-based approach above).
    >
    > But nothing in the sample you posted justifies not allowing
    > instances of class A to have a default-constructed, empty state.
     
    Siemel Naran, Apr 30, 2004
    #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. Mantorok Redgormor

    stylistic prototype question

    Mantorok Redgormor, Oct 29, 2003, in forum: C Programming
    Replies:
    5
    Views:
    353
    Mark A. Odell
    Oct 30, 2003
  2. Andrew Koenig

    Stylistic question about inheritance

    Andrew Koenig, Mar 31, 2005, in forum: Python
    Replies:
    17
    Views:
    490
    Guy Bolton King
    Apr 1, 2005
  3. Mark Healey

    A stylistic question.

    Mark Healey, May 14, 2006, in forum: C Programming
    Replies:
    60
    Views:
    1,241
    Jordan Abel
    May 28, 2006
  4. Guest
    Replies:
    0
    Views:
    447
    Guest
    Sep 14, 2005
  5. Bill Cunningham

    stylistic question

    Bill Cunningham, Nov 10, 2012, in forum: C Programming
    Replies:
    16
    Views:
    384
    Stephen Sprunk
    Nov 12, 2012
Loading...

Share This Page