Access to enum values ​​in C ++ 98 and C ++ 11

I have a set of enumeration values ​​defined in the "Foo" class (below).

namespace Fii
{
    class Foo 
    {
        struct Bar
        {
            enum Baz
            {
                BAZ1,
                BAZ2, 
                BAZ3
            };
        };
    };
};

I use struct to reduce the range of the Baz enumeration, and also show that there is a group of related values.

My goal is to assign an enum type value to a variable. Using the class definition above, you can do the following:

Fii::Foo::Bar::Baz myValue = Fii::Foo::Bar::BAZ1 (Works in both C++98 and C++11)

However, I feel that:

  • At first glance, myValue seems to be initialized as Fii :: Foo :: Bar, but this is only because the enumeration is a hack for grouping related constants in the parent (Bar in this case)

To increase availability, I re-named the code:

namespace Fii
{
    class Foo 
    {
        enum Baz
        {
            BAZ1,
            BAZ2, 
            BAZ3
        };
    };
};

Using this new class definition, you can do the following:

Fii::Foo::Baz myValue = Fii::Foo::Baz::BAZ1 (Works in C++11 only)
Fii::Foo::Baz myValue = Fii::Foo::BAZ1 (Should work on C++98 and C++11 - not tested)

Q1) Why is Fii :: Foo :: Bar :: Baz myValue = Fii :: Foo :: Baz :: BAZ1 only works in C ++ 11?

Q2) ++ 98 Fii:: Foo:: Baz myValue = Fii:: Foo:: Baz:: BAZ1? , .

: - Clang ++ 11 - Xcode 4 - Mac OS OS 10.8

+5
2

juanchopanza, Q1...

Q2: ++ 98 Fii:: Foo:: Baz myValue = Fii:: Foo:: Baz:: BAZ1? .

- :

namespace Fii
{
    class Foo
    {
        class Baz
        {
          public:
            enum E { BAZ1, BAZ2, BAZ3 };
            Baz(E e) : e_(e) { }
            operator const E() const { return e_; }
          private:
            E e_;
        };
    };
}

: Fii::Foo::Baz::BAZ1 ++ 03, Baz namespace class/struct/union. , Baz , BAZ1 . , Baz (/struct), , . , , , e_ , Baz get() const.

+5

++ 11 . enum , .

enum Foo { FOO1, FOO2, FOO3 }; // old-style enum

Foo f1 = Foo::FOO1; // OK in C++11, error in C++98.
Foo f2 = FOO1; // OK in C++98 and C++11 (for backward compatibility)
+6

All Articles