C++ named index array initialisation

Started by
7 comments, last by walkingcarcass 17 years, 9 months ago
Hi all. The code below is a snip from a simple scripting language I'm playing with:

static const unsigned int GOTO		= 0;
static const unsigned int TRIGGER	= 1;
static const unsigned int SIGN		= 2;
// etc...

static const unsigned int number_of_arguments[NUMBER_OF_OPCODES] = {
	[GOTO] = 1,
	[TRIGGER] = 1,
	[SIGN] = 2,
// etc...
};

This doesn't compile -- "expected primary-expression before '[' token". The syntax was inspired by something I saw in the Linux kernel source but didn't understand (and can't find anymore). C++ tutorials don't seem to mention it. I take it I've gotten the syntax a bit wrong, but I can't find an example of the right way to do it. Help, please! It's a big array and I don't fancy keeping track of the indices by eye.
spraff.net: don't laugh, I'm still just starting...
Advertisement
This is not valid C++ syntax. You cannot initialise an array like that.
NB: "static" should not be used anymore to indicate local variables, use an anonymous namespace instead.
namespace {    const unsigned int GOTO    = 0;    const unsigned int TRIGGER = 1;    const unsigned int sign    = 2;    // ...    // there is no way in C++ to use custom indexing in an array initialiser...    const unsigend int number_of_arguments[NUMBER_OF_OPCODES] = {          1,       // GOTO          1,       // TRIGGER          2,       // SIGN          // ...    };}

Or, do your initialisation in a function:

enum { GOTO=0,TRIGGER,SIGN }; // no need to explicitly state indiciesint args[NO_OF_ARGS];void init(){    args[GOTO]=1;    args[TRIGGER]=1;    args[SIGN]=2;}


then call init() somewhere at the start.
On a side node, it might be more convenient to use an enum for your constants, ie.
enum Opcodes {GOTO, TRIGGER, SIGN};
instead of what you're doing now.
In case you were wondering what to put in your next christian game; don't ask me, because I'm an atheist, an infidel, and all in all an antitheist. If that doesn't bother you, visit my site that has all kinds of small utilities and widgets.
Also, at least on C++, "static const" is a tautology because global constants have implicitly internal linkage (ie. they are already static). No anonymous namespaces needed, either.

But to emphasize, an enum is the way to go.
walkingcarcass - this is legal in C99. GCC allows it as an extension in C89, but not in C++.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I dont think you can use goto as a variable.
slymrHopefully game is in progress.
You can if it is in caps (or has any caps in it). GameDev syntax highlighting obviously does not account for this. For Shame!!! [smile]
Damn shame. I've been looking for a better way to initialise arrays for ages and I thought I had it. Oh well. Thanks all
spraff.net: don't laugh, I'm still just starting...

This topic is closed to new replies.

Advertisement