Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > array initialization in initialization list.

Reply
Thread Tools

array initialization in initialization list.

 
 
toton
Guest
Posts: n/a
 
      09-28-2006
Hi,
I can initialize an array of class with a specific class as,
class Test{
public:
Test(int){}
};
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?
like
class TestContainer{
private:
Test _x;
public:
TestContainer(int size) : _x(Test(size) {}
};
This works.
class TestContainer{
private:
Test _x[2];
public:
TestContainer(int size) : ??? {} //how to do it like array
initialization?
};
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?
Thanks
abir

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-28-2006
toton wrote:
> Hi,
> I can initialize an array of class with a specific class as,
> class Test{
> public:
> Test(int){}
> };
> 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?


No.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      09-28-2006
On 28 Sep 2006 03:50:03 -0700 in comp.lang.c++, "toton"
<(E-Mail Removed)> wrote,
> private:
> Test _x;
> public:
> TestContainer(int size) : _x(Test(size) {}


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
at:
http://www.parashift.com/c++-faq-lite/

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      09-28-2006
toton wrote:
> Hi,
> I can initialize an array of class with a specific class as,
> class Test{
> public:
> Test(int){}
> };
> 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?


You can't with arrays, but you can with std::vector (which you should
probably be using anyway,
http://www.parashift.com/c++-faq-lit...html#faq-34.1). For
instance:

#include <vector>
using namespace std;

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

Initializer& Add( const T& t )
{
v_.push_back(t);
return *this;
}

operator vector<T>() const
{
return v_;
}
};

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

Cheers! --M

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      09-28-2006

toton wrote:
> But my question is, In object initializer list can a array
> initialization be called? If yes, what is the syntax?


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

Gavin Deane

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-28-2006
Gavin Deane wrote:
> toton wrote:
>> But my question is, In object initializer list can a array
>> initialization be called? If yes, what is the syntax?

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


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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
Initialization of an unconstrained array object to the null array jens VHDL 3 08-19-2008 02:29 AM
static (non-zero) initialization of an array of structs (array of length #define LEN) anon.asdf@gmail.com C Programming 23 08-28-2007 11:54 PM
Array initialization when defining the array Spiro Trikaliotis C Programming 5 06-09-2007 09:38 AM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments