mercredi 9 mai 2018

C++11 Overloading operator for in class placed enum class

this is my first try to use enum classes for my projects, but I have the problem that I can't compile my code if the enum class is placed inside of another class. I try to define the operator overloading like my example and I try to do it outside, too. All works fine if I place the enum class outside the class. Whats wrong? How to overloading the operator if I what to use it placed in an class?

#include <cstdint>

namespace MyNamespace
{
    class MyClass
    {
    public:
        enum class MyEnum_t
        {
            VALUE_0 = 0x0,
            VALUE_1 = 0x1,
            VALUE_2 = 0x2,
            VALUE_3 = 0x4,
            VALUE_4 = 0x8 
       };

        inline MyEnum_t &operator|(MyEnum_t lhs, MyEnum_t rhs)
        {
            return static_cast<MyEnum_t>(static_cast<std::uint8_t>(lhs) | static_cast<std::uint8_t>(rhs));
        }
}

int main()
{
    MyNamespace::MyClass::MyEnum_t test = MyNamespace::MyClass::MyEnum_t::VALUE_0;

    test = MyNamespace::MyClass:MyEnum_t::VALUE_1 | MyNamespace::MyClass::MyEnum_t::VALUE_2;

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire