473,804 Members | 3,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(i nt size) : _x(Test(size) {}
};
This works.
class TestContainer{
private:
Test _x[2];
public:
TestContainer(i nt 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(i nt 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 24324
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*******@gmai l.comwrote,
private:
Test _x;
public:
TestContainer(i nt size) : _x(Test(size) {}
Should be
TestContainer(i nt 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 "initializa tion 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<typena me 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<dou ble>( 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
initializati on 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
27189
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
5941
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
16
41780
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, but is it legal? Most compilers accept it, but one doesn't recognize the rhs as a constant. What are the requirements for the rhs in the declaration of a const pointer? Is the following program legal C? int main(int argc, char *argv) {
8
3691
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 static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
10
3039
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
3
2639
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 and I am using is upto 10 or less. Because of that I find 990 reset of them as un initialized objects, when I extract the information from the Object it will throw an exception also and I feel unnecessarily I am wasting of memory. So I need dynamic...
15
3382
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
2199
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 this really valid c++ or c? where can I find out how this is implemented? Im concerned because I had a 3rd party library wrapper which was
2
3494
by: aaragon | last post by:
Hello everyone, Is this valid? template <class A> struct ClassA { typedef A value_type; value_type* data_;
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7616
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.