Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use nested enum of one class as a nested enum of another?

Tags:

c++

enums

The code bellow will give compile error on line enum en = A::en; but it describes what I want to do (to make nested enum of A to be nested enum of B as well).

#include <iostream>
using namespace std;
struct A
{
    enum a_en{X = 0, Y = 1};
};
struct B
{
    enum b_en = A::a_en; //syntax error
};
int main()
{
    cout << B::X << endl;
    return 0;
}

So the question is how can I do such thing in c++?

like image 450
Mihran Hovsepyan Avatar asked Aug 03 '11 09:08

Mihran Hovsepyan


People also ask

Can you have nested enums?

We can have a nested enum type declaration inside a class, an interface, or another enum type.

Can enum have two same values python?

By definition, the enumeration member values are unique. However, you can create different member names with the same values.

Can enums inherit Java?

Java Enum and Interface As we have learned, we cannot inherit enum classes in Java. However, enum classes can implement interfaces.


1 Answers

Put the enum in a base class that both A and B can inherit from.

like image 83
Bo Persson Avatar answered Oct 18 '22 15:10

Bo Persson