mardi 27 juillet 2021

Experiments with namespace in class protected area

#include <iostream>

class A
{
public:
    int func();

protected:
    namespace B { enum { D = 0, E = 1 }; }
    namespace C { enum { D = 0, E = 1 }; }
}

int A::func()
{
    int x = A::B::D;
    int y = A::C::E;
    return x + y;
}

int main() {
    A a;
    int x = a.func();
    std::cout << x << std::endl; // 1
    
    return 0;
}

What's wrong with this code?

I am just curious and experimenting with namespace, because I'd like to have enums with same names of values.

I don't want to use enum class, because I cannot do operations with integer without overloading operators or casting.

Aucun commentaire:

Enregistrer un commentaire