midrare
midrare

Reputation: 2764

enum members of members in C++, or alternative

I want to use C++ to create an enum whose members have members.
I had a similar question here, but that one dealt with D, not C++.


In Python, I can do this:

class Product(enum.Enum):
    PHOTOSHOP = "Image editor.", 0
    FIREFOX = "Web browser.", 1
    NOTEPAD = "Text editor.", 2

    def __init__(self, description, num):
        self.description = description
        self.num = num
>>> print(Product.PHOTOSHOP.description)
>>> "Image editor."

Java can do something like this:

public enum Product {
    PHOTOSHOP("Image editor.", 0),
    FIREFOX("Web browser.", 1),
    NOTEPAD("Text editor.", 2);

    private final String description;
    private final int num;

    Product(String description, int num) {
        this.description = description;
        this.num = num;
    }
}

Can I do this in C++?
If something to this effect can't be done in C++, what's a good alternative?

Upvotes: 1

Views: 122

Answers (3)

bobah
bobah

Reputation: 18864

I realize it does not answer the question, but a full featured enum inevitably requires passing around a reference to a singleton instead of just a value (thus affecting spacial locality of the code). That would not be the C++ way.

The C++ way would be to only pay the price when you have to. For instance:

enum class X { ... };
char const* to_str(X x) { return lookup_value_in_static_map(x);}
X from_str(char const* v)
ostream& operator <<(ostream& out, X x) { return out << to_str(x); }

The above can be made compile time friendly with a few easy tweaks if needed.

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283624

By going to a struct rather than an enum, you can accomplish something similar:

struct Product
{
    static constexpr struct type {const char* const description; const int num;}
        PHOTOSHOP{"Image editor.", 0},
        FIREFOX{"Web browser.", 1},
        NOTEPAD{"Text editor.", 2};
};

Unfortunately you can't use std::string, since it isn't a "literal" type, and thus not eligible for use in constexpr objects.

Another issue with this is that the type of the enumerands is Product::type, not just Product, and this will affect any code where you need to declare a variable. It's just not possible to use inline definition and also have the same type for the items as the type that contains them.

Upvotes: 1

Fibbs
Fibbs

Reputation: 1420

As far as I'm aware you can't have multi-component enums in C++, though I don't claim to be an expert.

What you can do however is declare an enum and use it to look up strings in an array.

enum Product
{
    PHOTOSHOP = 0,
    FIREFOX,
    NOTEPAD,
    // must always be last
    PRODUCT_ENUM_SIZE
};

const char* Product_Descriptions[PRODUCT_ENUM_SIZE];
Product_Descriptions[PHOTOSHOP] = "Image Editor";
Product_Descriptions[FIREFOX] = "Web Browser";
Product_Descriptions[NOTEPAD] = "Text Editor";

std::cout << "Firefox product number: " << FIREFOX << ". Description: " << Product_Descriptions[FIREFOX] << std::endl;

Upvotes: 1

Related Questions