Coder89
Coder89

Reputation: 13

Same identifier in an 2 different enum in same namespace in different files

I am using same namespace in two different files. In those files i have 2 enum. I need to use same identifiers in those enum. Is there a way to do it?

In first file first.h

...
namespace foo
{
 enum direction
 {
   NONE, 
   RIGHT    
   ...
 }
}

In second file
second.h

...
namespace foo
{
 enum reverse
 {
   NONE,  
   LEFT,   
   ...
 }
}

This is a sample code. I need to reuse "NONE". I need code for visual studio 2008. (Cannot use : enum class)

Upvotes: 1

Views: 2373

Answers (4)

Tristan Brindle
Tristan Brindle

Reputation: 16824

If you can use C++11, the best way to achieve what you want is to use "strongly typed enums", declared using the enum class keywords, i.e.

enum class direction
{
    NONE,
    LEFT
};

enum class reverse
{
    NONE,
    RIGHT
};

In the rest of your code, you then refer to the enumerators as ns::direction::NONE and ns::reverse::NONE (like in C#, if you're familiar with that language).

If you don't have C++11 available, then you can simulate the same thing by declaring an anonymous enum in an inner namespace, for example

namespace direction {
     enum {
         NONE,
         LEFT
     };
}

namespace reverse {
    enum {
        NONE,
        RIGHT
    };
}

Again, you then refer to direction::NONE and reverse::NONE, so there is no conflict.

Upvotes: 5

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

You may use

direction::NONE

and

reverse::NONE

to distinguish the enumerators provided that the enumerations are defined in different scopes. Otherwise the compiler will issue an error of redefinition of an enumerator.

Here is an example

#include <iostream>

enum A { NONE };

namespace N1
{
    enum B { NONE = 1 };
}

int main() 
{
    enum C { NONE = 2 };

    std::cout << A::NONE << '\t' << N1::NONE << '\t' << NONE << std::endl;

    return 0;
}

The output is

0   1   2

Upvotes: 2

Mats Petersson
Mats Petersson

Reputation: 129454

no, you can't reuse the values from one enum to another.

This is probably NOT the right solution, but you can either use explicit enum values, e.g.

 enum ... 
 {
    NONE = 0,
    ...
 };

or rely on the compiler starting with 0 and counting up one for each enumeration (assuming no assignment).

However, they are still not the same enum type, and I think you should consider a different solution. It's not entirely clear from your question what you are actually trying to achieve, but it sounds like you are trying to make a "reverse control", where left becomes right and right becomes left, etc. I would suggest having a getter function that does something like this:

direction GetDirection()
{
    if (reverse)
    {
       switch(curDirection) 
       {
          case LEFT:
              return RIGHT;
          case RIGHT:
              return LEFT;
         ...  deal with other directions that need reversing ... 
       }
    }
    return curDirection;
}

Another option is to implement two classes, where one does a "reverse switch", and the other just returns the current direction without reversing.

Upvotes: 0

Alex F
Alex F

Reputation: 43321

#include <iostream>
#include "first.h"

namespace second
{
    #include "second.h"
}

using namespace std;

int main()
{
    cout << foo::NONE << " " << second::foo::NONE << endl;

    return 0;
}

Upvotes: 1

Related Questions