HelloHi
HelloHi

Reputation: 165

is it possible to nest to class existing enum

I have enum in some header file. Is it possible to nest to the class existing enum?

Explanation:

some headerfile.h:

enum someEnum
{
someValue
/*other values*/
};

other header:

#include "headerfile.h"
class someClass
{
 public:
  //using enum someEnum; //don't work as I want
 };

I want that someValue will be accesible as

     someClass::someValue 

So my question is it possible?

Upvotes: 1

Views: 82

Answers (3)

Michelle
Michelle

Reputation: 21

You could try to include the library inside the class definition.

Upvotes: 2

AndersK
AndersK

Reputation: 36082

well one way would be to do this:

class someClass
{
 public:

#include "headerfile.h"

  //using enum someEnum; //don't work as I want

 };

not pretty but works.

Upvotes: 2

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39370

You can nest that enum definition:

class someClass {
public:
    enum someEnum {
        someValue
    };
};

Then you can access this enumerations just like the way you wanted:

someClass::someEnum X = someClass::someValue;

If, however, what you wanted was to create a member variable typed someEnum, you can do it either by just supplying someEnum as a type, or nesting the enumeration and putting the variable name before the semicolon.

Upvotes: 3

Related Questions