Initialization of an array

J

jamx

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

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

Victor Bazarov

jamx said:
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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

jamx said:
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];
};
 
A

Alf P. Steinbach

* Victor Bazarov:
jamx said:
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.
 
P

Pavel

jamx said:
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 */
}
 
G

Grizlyk

Julian said:
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/
 
J

jamx

* Victor Bazarov:
jamx said:
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 ;-)
 
A

Alf P. Steinbach

* jamx:
* 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<int> some_array;
};
 
J

jamx

* jamx:


* 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<int> some_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.
 
G

Gavin Deane

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<int> v(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
 
G

Gavin Deane

jamx said:
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
 
P

peter koch

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];

};

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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

peter said:
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.
 
P

Pavel

Gavin said:
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<int> v(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
 
P

Pavel

Gavin said:
jamx said:
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
 
P

peter koch

Gavin said:
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<int> v(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top