dimanche 16 août 2020

Idiom for a C++11 enum with methods and static instances?

I'd like to do the following (which won't compile):

class Foo {
    int value;

    Foo(const int arg)
        : value{arg}
    {}

public:    
    static const Foo a{0};
    static const Foo b{1};
    static const Foo c{2};

    Foo func(const Foo& foo) {...}
};

so that the the instances of Foo are strongly controlled (like in an enum) and so that I can write code like auto foo = Foo::a; and auto foo = Foo::a.func(Foo::b);

Unfortunately, the code won't compile under C++11 because the static instances have incomplete type.

Is there an idiom for this?

Aucun commentaire:

Enregistrer un commentaire