CraftedGaming
CraftedGaming

Reputation: 529

C++ index of define goes towards index initialization number

I'm looking around the internet to see my problem. Either I can't see it or it does not exist. What I'm trying to do is I have a header file for a list of ids of items in my program. Now I wanted to make my life easier by having an index rather than having absolute numbers that whenever I change something I wouldn't have to rearrange everything again.

Current code:

#define id1 1
#define id2 2
#define id3 3
#define id4 4
//times 100 more

Proposed code:

short index = 1;
#define id1 index
#define id2 index++
#define id3 index++
#define id4 index++

However printing the said code would give me a result of this

4
3
2
1 

I'm weirded out by this. I see it going back towards the number that I initialized index with, which is one. I changed the index++ to index-- and the same result happened only that it now had negative values.

-2
-1
0
1 

Is there a way to make it easier when I'm incrementing the index here? or is there something I'm missing out?

Upvotes: 0

Views: 44

Answers (1)

Use an enum to define sequences. You must to set only the first element.

#include <iostream>
using std::cout; using std::endl;
int main()
{
  enum {id1=0, id2, id3, id4, id5 };

  cout << "Id1 value is " << id1 << endl;
  cout << "Id2 value is " << id2 << endl;
  cout << "Id3 value is " << id3 << endl;

}

Upvotes: 1

Related Questions