473,386 Members | 1,679 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,386 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 3339
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.