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++?
We can have a nested enum type declaration inside a class, an interface, or another enum type.
By definition, the enumeration member values are unique. However, you can create different member names with the same values.
Java Enum and Interface As we have learned, we cannot inherit enum classes in Java. However, enum classes can implement interfaces.
Put the enum in a base class that both A and B can inherit from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With