|
Initializing const arrays |
SiegeLord
Member #7,827
October 2006
|
I have a C style const array and I want to initialize at compile time (not like I have any choice in that matter) and I want to do it like this: const int arr[2]; arr[0] = 1; arr[1] = 2; Naturally, that does not compile. Is something like that impossible to do in C++? Note that I am well aware of how to initialize const arrays using the standard method. This is not what I want: const int arr[] = {1, 2};
"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Vanneto
Member #8,643
May 2007
|
const int arr[2] = {1, 2};
In capitalist America bank robs you. |
X-G
Member #856
December 2000
|
Too bad, that's what you're getting. -- |
SiegeLord
Member #7,827
October 2006
|
Very well, do I get any improvements if I drop the const requirement? "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
X-G
Member #856
December 2000
|
Depends on what you mean by "improvement". -- |
Wetimer
Member #1,622
November 2001
|
What's wrong with the standard method? Perhaps if you give more information we can suggest a way to do whatever you are trying to do. <code>if(Windows.State = Crash) Computer.halt();</code> |
kazzmir
Member #1,786
December 2001
|
Here is a fun gcc extension const int x[] = { [0] = 1, [3] = 9 }; Which means exactly what it looks like, initialize element 0 to 1, and element 3 to 9. All other elements are 0. |
SiegeLord
Member #7,827
October 2006
|
Woah... For now I'll end up using this:
Never said it has to be pretty. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Wetimer
Member #1,622
November 2001
|
How in the world is that better then doing const int arr = { /* 0 */ 0, /* 1 */ 3, /* 2 */ 2, /* 3 */ 0 };
<code>if(Windows.State = Crash) Computer.halt();</code> |
ReyBrujo
Moderator
January 2001
|
Because you can modify those values at compile time with -DEN1=10 -- |
SiegeLord
Member #7,827
October 2006
|
I was planning on using this for VTables, like this:
(Syntax might be a little off, I did not try to compile that) The idea behind the above code is that I assign an index of the function to a constant simultaneously and in a flexible fashion: i.e. so it would be easy for me to add more enemy types. I also wanted to do this at compile time so I wouldn't have to have an Init() function to fill in my array. In the end I figured that it is better to just store the function pointers in the SEnemy struct itself. A little less headache that way. EDIT: "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
TeamTerradactyl
Member #7,733
September 2006
|
Is there a reason you don't want to simply use an enum? enum EnemyTypes { arr1 = 1, arr2 = 2, ... } EnemyTypes; Then once you add more enemies, update EnemyTypes with your new types. enum is even generous enough that you can define them to be non-linear: enum EnemyTypes { arr1 = 1, arr2 = 15, arr3 = 17; } EnemyTypes; It may not be what you want, but I don't understand exactly why you want a const like that; just #define it if you have to, instead of using "const int ..."
|
SiegeLord
Member #7,827
October 2006
|
That's what I originally had. However, to create the VTables I needed to have an init function to fill it out with functions: enum { TallEnemy, ShortEnemy }; int (fns*)()[MAX_TYPES]; void Init() { fn[TallEnemy] = Fn1; fn[ShortEnemy] = Fn2; } But I did not like that, as it sort of implied that the array of functions was somehow dynamically alterable... Therefore I wished for it to be const, and defined at compile time. I have since abandoned that approach, VTables are too annoying to reimplement and I gain no advantage to not doing them the C++ way. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Thomas Fjellstrom
Member #476
June 2000
|
Quote: I gain no advantage to not doing them the C++ way. Pretty much. If you use C++, you might as well just use C++ features. -- |
Arthur Kalliokoski
Second in Command
February 2005
|
If the variables are in some sort of mathmatical relationship (so they can be computed) you could make a little console proglet to write your variables to a file, then paste the result into your source. like: EDIT They all watch too much MSNBC... they get ideas. |
|