array declaration and initialization in classes

Hello knowledgebearers out there,

just visiting a training course in c++ I neither can solve nor find any hint within the cplusplus-site regarding the following problem:

designing a class I want to declare a two-dimensional array. But I don't want to tell the .h-file the size of the array, just typing

static const int xy[][];

The size I want to tell within the .cpp-file via

const int xy[][] = {{31,28,31},{30,19,28}};

The same procedure works fine for one dimensional arrays, but with two dimensions I'm getting compiler-errors.

Any suggestions?

Thanks,
Rainer
A two dimensional array is made of rows and columns, just curious, why would you not want to specify that in main (or the .cpp file you are using it in).
i.e. const int xy[2][4] = {{31,28,31},{30,19,28}};
const int xy[2]/*2 rows*/[4]/*4 columns*/ = ....
are you wanting a dynamic two dimensional array ?
I want to use the array as a class-constant, therefore I declare it within the header file (where I can't initialize it...) and implement it within the .cpp file.

But I'm not yet sure, how many elements I need within the array, therefore I would prefer to declare it as mentioned without noting the size in the header file, but just typing the elements needed with the initialization.

That is what I know so far along my education...

Rainer
what you are describing is dynamically allocating an array. [html]http://en.wikipedia.org/wiki/Dynamic_array[/html]
[html]http://www.youtube.com/watch?v=zxL_cTsO8ks&feature=channel[/html]

dynamic allocated memory is "on the fly" memory. Like when using an array that you are not sure how big you need it to be. (i.e. not sure how many elements/size) Then you can dynamically allocate it and the size is allocated at run time. These are some good links to get you started. Maybe some code to analyze would also be helpful soon. Hope this helps to get you started.
Sorry, my description might be confusing (I'm not a native English-speaker... ;))

The size of the array will be fixed, but during developement of my code I don't know, what will be the size at end of program development. So, when I find I need one more element within my array, I simply want to add that element once into the initialization list, recompile and let it run.

When I declare the size of the array explicitly, I need additionally to increase (or decrease) the value of the size-declaration - which I might forget eventually, resulting in faulty compilations and time-consuming debugging of trivial errors.

Therefore I would prefer to declare the array like
static const int xy [][]
within the header file and initialize it within the .cpp-file like
const int xy = {{31,28,31},{30,19,28}};
(but I find, that syntax is not accepted, so how to achieve that behaviour?)

If later during program development I find, it works better with one more element inside the array, I rewrite the array-initalization like
const int xy = {{31,28,31,52},{30,19,28,17}};
and recompile the file. Otherwise I would need to change ...xy[2][3] --> ...xy[2][4], both inside the header- and within the .cpp-file - which I might forget before compilation...

I hope it's now better to understand, what I want to do...?

Rainer
Last edited on
Topic archived. No new replies allowed.