Jump to content
  • Advertisement
Sign in to follow this  
walkingcarcass

C++ named index array initialisation

This topic is 4437 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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
// ...
};
}


Share this post


Link to post
Share on other sites
Or, do your initialisation in a function:


enum { GOTO=0,TRIGGER,SIGN }; // no need to explicitly state indicies

int args[NO_OF_ARGS];

void init()
{
args[GOTO]=1;
args[TRIGGER]=1;
args[SIGN]=2;
}



then call init() somewhere at the start.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!