Mihran Hovsepyan
Mihran Hovsepyan

Reputation: 11088

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

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++?

Upvotes: 6

Views: 1464

Answers (3)

Ajay
Ajay

Reputation: 18411

When the classes/structs are related this way, you should inherit them. Put the public enum in base class so that all derived (related) classes can access it.

MFC' CFile class has enums defined, which CStdioFile and other derived classes can use:

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

Upvotes: 0

asv
asv

Reputation: 181

Use

struct B: A
{
};

instead of

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

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92241

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

Upvotes: 5

Related Questions