Blade3
Blade3

Reputation: 4360

Enums inside of a C++ class

I am trying to use a class that has an enum type declared inside the class like so:

class x {
public:
    x(int);
    x( const x &);
    virtual ~x();
    x & operator=(const x &);
    virtual double operator()() const;

    typedef enum  {
        LINEAR = 0,      /// Perform linear interpolation on the table
        DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
    } XEnumType; 
};

I need to declare an instance of this class and initialize the enum type. I come from C# and normally see enums declared OUTSIDE of a class, not INSIDE like it is here. How do I initialize the enum type. For example, I want to do something like this:

x myX(10);   
myX.XEnumType = Linear;

Obviously this doesn't work. How would I do this?

Upvotes: 6

Views: 43435

Answers (6)

Heisenbug
Heisenbug

Reputation: 39164

You declared a new type : XEnumType. You have to create a field of that type inside x class. . For example:

class x {

public:
    x(int);
    x( const x &);
    virtual ~x();
    x & operator=(const x &);
    virtual double operator()() const;
typedef enum  {
    LINEAR = 0,      /// Perform linear interpolation on the table
    DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
} XEnumType; 
public:
XEnumType type;
};

Then you can access to it that way:

x foo(10);
foo.type = LINEAR;

Upvotes: 2

Martin Hennings
Martin Hennings

Reputation: 16846

Let me first assume some preconditions:

  • Class x is from a third-party library and thus cannot be changed.
  • Class x defines some integer constants with the help of an enum.
  • Class x is supposed to be initialized with either one of the constants LINEAR or DIPARABOLIC.

Your problem is that these constant values are declared within class x. So to initialize an instance of x you need to specify the scope:

Instead of

x myX(10);   
myX.XEnumType = Linear;

try

x myX(x::LINEAR);

By specifying x:: you provide the scope of the constant.

Upvotes: 0

mcorley
mcorley

Reputation: 767

enum XEnumType {
    LINEAR, DIPARABOLIC
};

class x {    
    public:    
      x(int);    
      x( const x &);    
      virtual ~x();    
      x & operator=(const x &);    
      virtual double operator()() const;    
      XEnumType my_enum;
};

Usage:

x myX(10);
myX.my_enum = LINEAR;

Upvotes: 5

Nim
Nim

Reputation: 33655

the line

typedef enum  {
    LINEAR = 0,      /// Perform linear interpolation on the table
    DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
} XEnumType; 

defines a type called XEnumType, actually this is redundant anyway - prefer something like:

enum XEnumType
{
  LINEAR = 0,      /// Perform linear interpolation on the table
  DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
};

Now you need to define a member of this type in your class

XEnumType _eType;

In your constructor, then you can initialize to whatever

x::x(int ) : _eType(x::LINEAR) {}

Upvotes: 0

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506985

First: Don't use typedef. Instead, put the name of the enumeration in its head

enum XEnumType {
    LINEAR = 0,      /// Perform linear interpolation on the table
    DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
};

In a nutshell, doing like you did will behave mostly the same, but in arcane corner cases will be different. The syntax you used will behave very different from the syntax I used above only in C.

Second: That just defines a type. But you want to define an object of that enumeration. Do so:

XEnumType e;

In summary:

class x {
    /* ... stays the same ... */

    enum XEnumType {
        LINEAR = 0,      /// Perform linear interpolation on the table
        DIPARABOLIC = 1  /// Perform parabolic interpolation on the table
    }; 

    XEnumType e;
};

void someFunction() {
    x myX(10);
    myX.e = x::LINEAR;
}

Upvotes: 9

Dan F
Dan F

Reputation: 17732

First you need to declare a variable that is of the type XEnumType within your class Then you can access the actual enumeration values using the class name for scope: x::LINEAR or x::DIPARABOLIC

class x{
 //Your other stuff

XEnumType myEnum;
};

int main(void)
{
    x myNewClass();
    x.myEnum = x::LINEAR;
}

Upvotes: 9

Related Questions