473,412 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

array initialization in initialization list.

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

Sep 28 '06 #1
5 24291
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
Sep 28 '06 #2
On 28 Sep 2006 03:50:03 -0700 in comp.lang.c++, "toton"
<ab*******@gmail.comwrote,
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/

Sep 28 '06 #3
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<Tv_;
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<doublev_;
public:
Example( double d0, double d1, double d2 )
: v_( Initializer<double>( 3 )
.Add(d0)
.Add(d1)
.Add(d2) )
{}
// ...
};

Cheers! --M

Sep 28 '06 #4

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

Sep 28 '06 #5
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
Sep 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
10
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
3
by: sk.rasheedfarhan | last post by:
Hi , Here I am new user to C#, my problem is I have to use dynamic Array of objects. But I heard C# don't support ptrs (using managed code C# support). In short i initialized objects of 1000...
15
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
24
by: DomoChan | last post by:
the code below will compile in visual c++ 2003, but im not sure its valid. unsigned char myString = ""; after this line executes, all the bytes within myString are indeed set to '0's' but is...
2
by: aaragon | last post by:
Hello everyone, Is this valid? template <class A> struct ClassA { typedef A value_type; value_type* data_;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.