![]() |
Re: Stylistic question -- initialization lists and vectors
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 |
Stylistic question -- initialization lists and vectors
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 |
Re: Stylistic question -- initialization lists and vectors
"Russell Silva" <naejulak@yahoo.com> wrote in message
news:fe286dec.0404292133.e66a6f2@posting.google.co m... .... > 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 |
Re: Stylistic question -- initialization lists and vectors
Denis Remezov <REMOVETHISdenis_remezov@yahoo.removethis.ca> wrote in message news:<4091B737.19EC397B@yahoo.removethis.ca>...
> 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 |
Re: Stylistic question -- initialization lists and vectors
"Ivan Vecerina" <please_use_web_form@ivan.vecerina.com> wrote in message
> "Russell Silva" <naejulak@yahoo.com> 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. |
| All times are GMT. The time now is 02:22 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.