How to use a nested enumeration of one class as a nested enumeration of another?

The following is the error code in the line enum en = A::en; but it describes what I want to do (so that nested enumeration A nested enumeration B ).

 #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 a thing in C ++?

+6
source share
3 answers

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

+5
source

Using

 struct B: A { }; 

instead

 struct B { enum b_en = A::a_en; }; 
0
source

When classes / structures are connected in this way, you must inherit them. Put the public enumeration in the base class so that all the derived (related) classes can access it.

MFC ' CFile has specific enumerations that CStdioFile and other derived classes can use:

 enum OpenFlags { modeRead = (int) 0x00000, modeWrite = (int) 0x00001, ... }; 
0
source

Source: https://habr.com/ru/post/894175/


All Articles