Jump to content
  • Advertisement
Sign in to follow this  
sipickles

[c++] Something better than enum

This topic is 3625 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, After using python a while, I have become frustrated by C++ enum. I'd simply like namespaced enumerations. In python, its easy:
#python
class Names:
    TOM, DICK, HARRY = range(3)


assert Names.TOM == 0
This makes debug functions easy too:
#python
class Names:
    TOM, DICK, HARRY = range(3)

    AsString = ("TOM", "DICT", "HARRY")

print Names.AsString(Names.TOM)
Is there a simple way to achieve a similar thing in c++? Thanks

Share this post


Link to post
Share on other sites
Advertisement

namespace Names
{
enum { TOM, DICK, HARRY };
const char* AsString[] = {"TOM", "DICT", "HARRY"};
}

assert(Names::TOM == 0);
cout << Names::AsString[Names::TOM];

Share this post


Link to post
Share on other sites
THanks for the answer

While this is true, this would cause an error.

 enum Names { TOM, DICK, HARRY };
enum otherNames { TOM, BOB };


The enums are not actually namespaces.

Plus I really want the option to output a related string.

Share this post


Link to post
Share on other sites
Quote:
Original post by sipickles
THanks for the answer

While this is true, this would cause an error.

 enum Names { TOM, DICK, HARRY };
enum otherNames { TOM, BOB };


The enums are not actually namespaces.

Plus I really want the option to output a related string.


I don't understand either of your complaints. The code that Mike nl provided does use a namespace, so you don't have the collision problem. And what do you mean by "a related string"?

Share this post


Link to post
Share on other sites
I fail to see the problem, this compiles and runs fine:

#include <iostream>
using namespace std;

namespace Names
{
enum { TOM, DICK, HARRY };
const char* AsString[] = {"TOM", "DICT", "HARRY"};
}

namespace OtherNames
{
enum { TOM, DICK, HARRY };
const char* AsString[] = {"TOM2", "DICT2", "HARRY2"};
}

int main()
{
cout << Names::AsString[Names::TOM] << endl;
cout << OtherNames::AsString[OtherNames::TOM] << endl;
return 0;
}


and produces:

TOM
TOM2

Share this post


Link to post
Share on other sites
alvaro: Mikes example has been edited

Mike: I was replying to your first response, not the edited version. anyway Thanks! is it really so simple? I guess I had messed that up somehow!

I was try to make a class and add an enum etc etc oops

Share this post


Link to post
Share on other sites
Quote:
Original post by sipickles
alvaro: Mikes example has been edited

I see. There was no mark of editing, so he must have made the change before you posted.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!