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?
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;
}
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;
}
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;
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;
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) {}
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 constantsLINEAR
orDIPARABOLIC
.
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.
精彩评论