By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

Initialization of an array

P: n/a
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 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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];
};
You cannot.

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

P: n/a
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];
};
You can't do it directly, but you can use some like this:

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
Feb 25 '07 #3

P: n/a
* Victor Bazarov:
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];
};

You cannot.
Well, you can zero-initialize it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 25 '07 #4

P: n/a
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 */
}
Feb 26 '07 #5

P: n/a

Julian Albo wrote:
>
>How can you initialize an array, in the initialization list of a
constructor ??

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

You can't do it directly, but you can use some like this:

class Init
{
public:
Init (int (& array ) [2])
{
array [0]= 42;
array [1]= 43;
Note: it is assignment rather than initialization: "default ctor +
operator=".

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 26 '07 #6

P: n/a
On 25 feb, 22:29, "Alf P. Steinbach" <a...@start.nowrote:
* Victor Bazarov:
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];
};
You cannot.

Well, you can zero-initialize it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Thats what i want, to initialize them with zero ;-)

Feb 26 '07 #7

P: n/a
* jamx:
On 25 feb, 22:29, "Alf P. Steinbach" <a...@start.nowrote:
>* Victor Bazarov:
>>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];
};
You cannot.
Well, you can zero-initialize it.
Thats what i want, to initialize them with zero ;-)
Please don't quote signatures -- corrected.

In standard C++ you just write

struct SomeClass
{
SomeClass(): some_array() {}
int some_array[2];
};

Some compilers (notably old Visual C++, IIRC) don't support that, i.e.
they're not standard-conforming, but for such compilers you can employ
the artificial base class trick:

struct SomeClassMembers
{
int some_array[2];
};

struct SomeClass: SomeClassMembers
{
SomeClass(): SomeClassMembers() {}
};

However, no matter whether your compiler is standard-conforming in this
respect or not, you're probably much better off using a std::vector than
a raw array, i.e.

struct SomeClass
{
SomeClass(): some_array(2) {}
std::vector<intsome_array;
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 26 '07 #8

P: n/a
On 26 feb, 09:41, "Alf P. Steinbach" <a...@start.nowrote:
* jamx:
On 25 feb, 22:29, "Alf P. Steinbach" <a...@start.nowrote:
* Victor Bazarov:
>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];
};
You cannot.
Well, you can zero-initialize it.
Thats what i want, to initialize them with zero ;-)

Please don't quote signatures -- corrected.

In standard C++ you just write

struct SomeClass
{
SomeClass(): some_array() {}
int some_array[2];
};

Some compilers (notably old Visual C++, IIRC) don't support that, i.e.
they're not standard-conforming, but for such compilers you can employ
the artificial base class trick:

struct SomeClassMembers
{
int some_array[2];
};

struct SomeClass: SomeClassMembers
{
SomeClass(): SomeClassMembers() {}
};

However, no matter whether your compiler is standard-conforming in this
respect or not, you're probably much better off using a std::vector than
a raw array, i.e.

struct SomeClass
{
SomeClass(): some_array(2) {}
std::vector<intsome_array;
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
It's for BSD/UNIX, so i'm using gcc. I will try out this Someclass() :
some_arra() {} construction, thanks.

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.

Feb 26 '07 #9

P: n/a
On 26 Feb, 12:31, "jamx" <debraban...@gmail.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
Feb 26 '07 #10

P: n/a
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

P: n/a
On Feb 25, 7:11 pm, Julián Albo <JULIANA...@terra.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

P: n/a
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

P: n/a
Gavin Deane wrote:
On 26 Feb, 12:31, "jamx" <debraban...@gmail.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

P: n/a
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
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
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::initialValues[DIM] = { 3, 1, 4, 1, 5, 9 };

Pavel
Feb 28 '07 #15

P: n/a
On Feb 28, 4:00 am, Pavel <nos...@nospam.comwrote:
Gavin Deane wrote:
On 26 Feb, 12:31, "jamx" <debraban...@gmail.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 discussion thread is closed

Replies have been disabled for this discussion.