1

I have enum inside public part of some class A. I want to use this enum in class B. Is it a way to make it accessible without specifying *A::enum_value*? So I can use just *enum_value*.

using A::enumname;

inside class B doesn't compile.

1
  • 1
    Do you can simply remove the enum from class A? Commented Apr 12, 2013 at 14:12

2 Answers 2

1
If you only need certain values, you can do:

struct B {
  static const A::enumname 
    enum_value1 = A::enum_value1,
    enum_value2 = A::enum_value2;  // etc.
};

If you can modify A, you can extract the enum into a base class that can be used by both A and B:

struct AEnum {
  enum enumname { enum_value1, enum_value2 };
};

struct A : AEnum {
};

struct B : BEnum {
};
Sign up to request clarification or add additional context in comments.

Comments

0

There's no good way to directly import all the enum values from one place to another. In this case I'd suggest wrapping A's enum into a nested class, and then typedeffing that into B.

For example:

class A
{
public:
    struct UsefulEnumName
    {
        enum Type { BAR };
    };
};

class B
{
    typedef A::UsefulEnumName UsefulEnumName;
    UsefulEnumName::Type foo() const { return UsefulEnumName::BAR; }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.