开发者

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
{
  开发者_C百科  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++?


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


Use

struct B: A
{
};

instead of

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


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,
...    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜