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...
};
C++ named index array initialisation
Hi all. The code below is a snip from a simple scripting language I'm playing with:
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.
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.
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:
then call init() somewhere at the start.
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.
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.
But to emphasize, an enum is the way to go.
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement