473,323 Members | 1,574 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,323 software developers and data experts.

Initializing multidimensional arrays in constructor

Hi all. Is there any way to initialize an entire array in the constructor.
The code below does not work. Thanks.

class x {
public:
int array[2][2];
x();

};

x::x() {
array = {{1,2},{3,4}};
}

Feb 22 '06 #1
3 16026
"Allan A." <AA@alphaville.com> wrote in message
news:dt*********@news4.newsguy.com...
: Hi all. Is there any way to initialize an entire array in the
constructor.

No, unfortunately, not in the current version of the C++ standard.
[ it is being proposed to allow this in the next revision of
the standard, but this is still years away. See for example
http://www.open-std.org/jtc1/sc22/wg...2005/n1919.pdf ]

: The code below does not work. Thanks.
:
: class x {
: public:
: int array[2][2];
: x();
:
: };
:
: x::x() {
: array = {{1,2},{3,4}};
: }

The actual (proposed) syntax for an initialization would
look like:
x::x()
: array( {{1,2},{3,4}} )
{ }

The example your provided uses assignment, not initialization,
to set the contents of the array. It could become legal as
well if(/once) the above proposal is integrated in the standard.
Equivalent code can (and has) be written today as individual
assignments to each element of the array.
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Feb 22 '06 #2
On Wed, 22 Feb 2006 15:37:53 +0000, Allan A. wrote:
Hi all. Is there any way to initialize an entire array in the constructor.
The code below does not work. Thanks.

class x {
public:
int array[2][2];
x();

};

x::x() {
array = {{1,2},{3,4}};
}


You could do this:

x::x() {
static int initData[2][2] = {{1,2},{3,4}};
memcpy(array, initData, sizeof(array));
}

- Jay

Feb 22 '06 #3
Allan A. wrote:
Hi all. Is there any way to initialize an entire array in the constructor.
The code below does not work. Thanks.

class x {
public:
int array[2][2];
x();

};

x::x() {
array = {{1,2},{3,4}};
}


You could use vector< vector<int> > and do something similar to this:

http://groups.google.com/group/comp....75a9bd905b06c0

Then the member could even be const if desired.

Cheers! --M

Feb 22 '06 #4

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

Similar topics

5
by: TheKeith | last post by:
I can't figure out why this doesn't work: --------------------------------------------- greeting = new Array(); greeting = "hey"; greeting = "bye"; trace(greeting + greeting);
2
by: Terry | last post by:
Hi, can someone plz tell me how multidimensional arrays (like a 2-D array) are stored in memory? Are they like single dimensional arrays? Stored sequentially in one "row", so to say? Thanks ...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
3
by: Ravi Singh (UCSD) | last post by:
Hello all I am trying to use jagged and multi-dimensional arrays in C++. In C# these work fine // for jagged arrays string jaggedArray = new string ; //for multidimensional arrays string...
12
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.