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

Home Posts Topics Members FAQ

Initialization of an array

How can you initialize an array, in the initialization list of a
constructor ??

SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};

Feb 25 '07
15 3381
On 26 Feb, 00:05, Pavel <nos...@nospam. comwrote:
jamx wrote:
How can you initialize an array, in the initialization list of a
constructor ??
SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};

I would simply initialize it in the constructor's body (actually, member
initialization is the constructor's bread-n-butter):
SomeClass() {
// *init some_array here */
That's the easiest way to provide an array with initial values, but it
isn't initialisation.

Member initialisation is indeed the constructor's bread and butter,
but you do it in the initialisation list (as the OP was trying to do),
not the constructor body. By the time you get to the constructor body
you've missed the opportunity to initialise anything. All you can do
there is assign and modify values.

Member arrays are a quirk because there's no way to initialise them
with a value of your choice in the initialiser list.

Gavin Deane

Feb 26 '07 #11
On Feb 25, 7:11 pm, Julián Albo <JULIANA...@ter ra.eswrote:
[snip]
>
class Init
{
public:
Init (int (& array ) [2])
{
array [0]= 42;
array [1]= 43;
}

};

class SomeClass : private Init
{
public:
SomeClass() : Init (some_array) { }
private:
int some_array[2];

};

--
Salu2
I do not like that hack. Technically, you assign to something whos
constructor has not yet run. For a POD type as above, this might not
matter, but the second the int [2] is changed to something with more
meat in it, you are sure to get into trouble no matter how forgiving
your compiler might otherwise be.

/Peter

Feb 26 '07 #12
peter koch wrote:
>class SomeClass : private Init
{
public:
SomeClass() : Init (some_array) { }
private:
int some_array[2];
};
I do not like that hack. Technically, you assign to something whos
constructor has not yet run. For a POD type as above, this might not
matter, but the second the int [2] is changed to something with more
meat in it, you are sure to get into trouble no matter how forgiving
your compiler might otherwise be.
This was a quick example. Other similar but less risky way can be:

class SomeClass
{
public:
SomeClass() : initsome (some_array) { }
private:
int some_array[2];
Init initsome;
};

But if the class is not so simple or is intended to be extended later, I
will not use those quick hacks, I will make some_array a class with a
constructor adequate to the needs, not an array.

--
Salu2
Feb 26 '07 #13
Gavin Deane wrote:
On 26 Feb, 12:31, "jamx" <debraban...@gm ail.comwrote:
>I agree, that most of the time a vector is the better choise. But
since pipe() requires an "int fildes[2]", i will use an array.

As it happens, none of that prevents you using a vector if you want
to.

pipe might say it takes an int fildes[2] but C and C++ don't let you
be that precise in function declarations. In fact, pipe takes just an
int* and it is entirely your responsibility to make sure that that
pointer points to the first element of an array with (at least) 2
elements. You can equally well do

vector<intv(2);
pipe(&v[0]);

which allows you to continue to use containers in your own code. Raw
arrays and pointers don't need to propogate outside the API you are
using.

Gavin Deane

The drawback to using std::vector though, for an array of 2 integers, is
100% memory overhead (ideally, but more probably something around 200%
for a standard allocator) and an extra chunk of dynamic memory. Not to
mention probable extra compilation time (every time :-)) and unnecessary
growth of a binary.

Now that we know more about the problem, I honestly do not see a better
alternative to simple

fildes[0] = fildes[1] = 0;

in the constructor's body.
Pavel
Feb 28 '07 #14
Gavin Deane wrote:
On 26 Feb, 00:05, Pavel <nos...@nospam. comwrote:
>jamx wrote:
>>How can you initialize an array, in the initialization list of a
constructor ??
SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};
I would simply initialize it in the constructor's body (actually, member
initializati on is the constructor's bread-n-butter):
SomeClass() {
// *init some_array here */

That's the easiest way to provide an array with initial values, but it
isn't initialisation.

Member initialisation is indeed the constructor's bread and butter,
but you do it in the initialisation list (as the OP was trying to do),
not the constructor body. By the time you get to the constructor body
you've missed the opportunity to initialise anything. All you can do
there is assign and modify values.

Member arrays are a quirk because there's no way to initialise them
with a value of your choice in the initialiser list.

Gavin Deane
Technically, you are right -- it is not an initialization of a member
(but btw some "SomeClass o(155);" IS an initialization of o even if
SomeClass is defined as.

class SomeClass {
int a[2];
public:
SomeClass(int initValue) { a[0] = a[1] = initValue; }
};

). For any practical purpose, however, I do not see a big difference
(unless a small additional time cost is significant -- in which case
whatever dirty tricks one can find would be justified). The only one I
could think of was a desire to use a brace-enclosed initializer list. It
is not very powerful construct in C++; if it is convenient for a
particular case, however, this is how it can be done, at the cost of a
single additional instance of a "prototype array" per running program:

// -- SomeClass header
class SomeClass {
enum { DIM = 6 };
static const int initialValues[DIM];
int a[DIM];
public:
SomeClass() { memcpy(a, initialValues, sizeof(a)); }
};

....

// -- SomeClass implementation module
const int SomeClass::init ialValues[DIM] = { 3, 1, 4, 1, 5, 9 };

Pavel
Feb 28 '07 #15
On Feb 28, 4:00 am, Pavel <nos...@nospam. comwrote:
Gavin Deane wrote:
On 26 Feb, 12:31, "jamx" <debraban...@gm ail.comwrote:
I agree, that most of the time a vector is the better choise. But
since pipe() requires an "int fildes[2]", i will use an array.
As it happens, none of that prevents you using a vector if you want
to.
pipe might say it takes an int fildes[2] but C and C++ don't let you
be that precise in function declarations. In fact, pipe takes just an
int* and it is entirely your responsibility to make sure that that
pointer points to the first element of an array with (at least) 2
elements. You can equally well do
vector<intv(2);
pipe(&v[0]);
which allows you to continue to use containers in your own code. Raw
arrays and pointers don't need to propogate outside the API you are
using.
Gavin Deane

The drawback to using std::vector though, for an array of 2 integers, is
100% memory overhead (ideally, but more probably something around 200%
for a standard allocator) and an extra chunk of dynamic memory. Not to
mention probable extra compilation time (every time :-)) and unnecessary
growth of a binary.
I normally argue strongly in favor of std::vector, but in the case
with small, fixedsize arrays I'd recommend something like
boost::array. I would not normally use built-in arrays because of
their many problems as "second-rate" citizens.

/Peter
>
Now that we know more about the problem, I honestly do not see a better
alternative to simple

fildes[0] = fildes[1] = 0;
I believe boost::array will do that for you automatically.

/Peter

Feb 28 '07 #16

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

Similar topics

1
3016
by: Piotr Sawuk | last post by:
just a quick question out of curiosity: how to initialize const arrays? struct srat { long num; ulong den; srat(){} } struct collisions
19
4563
by: Henry | last post by:
I finally thought I had an understanding of multi dimensional arrays in C when I get this: #include <stdio.h> #define max_x 3 #define max_y 5 int array;
6
13387
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
4
9691
by: Stephen Mayes | last post by:
I have initialized an array like this. const char matrix = { {0, 1, 2, 3}, {0, 1, 2}, {0, 1} }; gcc, (with no options set,) errors unless I specify
10
8566
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
5
24324
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {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?
23
3670
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and what kind of values are assigned to objects?
3
4913
by: jaime | last post by:
Hi all. The source code download bundle for "Beginning C: From Novice to Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress) contains a C source file (program9_09.c) which contains several instances of the following type of idiom: /* Program 9.9 REVERSI An Othello type game */ const int SIZE = 6;
5
9864
by: codeGhost | last post by:
I've been trying to ignore this issue for a while now, but I've come to the point in my code where I can't do so anymore. (For those of you who are wondering, this is NOT a homework question). Platform: VC++ 2002 System: Windows XP, sp3 ::First, here's the code in question:: char * cSection (const char* data, int start, int finish) {
0
10571
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
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10075
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
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...
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?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
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.