473,326 Members | 2,102 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,326 software developers and data experts.

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 #1
15 3338
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
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
* 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
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

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
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
* 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
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
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
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...@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
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...@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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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
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
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
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
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
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...
23
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...
3
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...
5
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). ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.