jeudi 21 février 2019

C++11: How to create an enum class inside an class that behaves like a sub class?

To explain my problem I posted an example below. The code in this form is not tested so there might be some syntax mistake in it. As I have to work with a lot of registers in an integrated circuit with their addresses which can be remapped, it would be very useful to create structures like that below. Is there some trick to create these structures? As this example does not work the way I want it because foo requires a Country object and Country::Europe::Italy is invalid as parameter.

// I want to create a structure like this
class myClass {
public:
    class Country {

        enum class Europe {
            England,
            France,
            Germany,
            Italy
        };

        enum class Asia {
            China,
            Japan
        };

    };

    // Here I want to make sure, that the method is only
    // called with a Country element and e.g. Italy should
    // behave like a Country object. Actually it should behave
    // as if it is derived from Country.
    int foo(Country c);

};



int main() {
    myClass myC();

    // Exemplary call of the method foo
    myC.foo(myClass::Country::Europe::Italy);
}

Aucun commentaire:

Enregistrer un commentaire