C++ 类中的枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5913359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enums inside of a C++ class
提问by Blade3
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:
我需要声明这个类的一个实例并初始化枚举类型。我来自 C#,通常会看到在类的外部声明的枚举,而不是像这里一样的 INSIDE。如何初始化枚举类型。例如,我想做这样的事情:
x myX(10);
myX.XEnumType = Linear;
Obviously this doesn't work. How would I do this?
显然这行不通。我该怎么做?
回答by Dan F
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
首先,您需要声明一个XEnumType
属于您的类中类型的变量,然后您可以使用作用域的类名访问实际的枚举值:x::LINEAR
或x::DIPARABOLIC
class x{
//Your other stuff
XEnumType myEnum;
};
int main(void)
{
x myNewClass();
x.myEnum = x::LINEAR;
}
回答by Johannes Schaub - litb
First: Don't use typedef
. Instead, put the name of the enumeration in its head
第一:不要使用typedef
. 相反,将枚举的名称放在其头部
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 mostlythe 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.
简而言之,像你那样做的行为大致相同,但在神秘的角落情况下会有所不同。您使用的语法将与我上面仅在 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;
}
回答by mcorley
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;
回答by Heisenbug
You declared a new type : XEnumType. You have to create a field of that type inside x class. . For example:
您声明了一个新类型:XEnumType。您必须在 x 类中创建该类型的字段。. 例如:
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;
回答by Nim
the line
线
typedef enum {
LINEAR = 0, /// Perform linear interpolation on the table
DIPARABOLIC = 1 /// Perform parabolic interpolation on the table
} XEnumType;
defines a typecalled XEnumType
, actually this is redundant anyway - prefer something like:
定义了一个名为的类型XEnumType
,实际上这是多余的 - 更喜欢这样的:
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) {}
回答by Martin Hennings
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
.
- 类
x
来自第三方库,因此无法更改。 - 类
x
在枚举的帮助下定义了一些整数常量。 - 类
x
应该使用常量之一LINEAR
或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:
您的问题是这些常量值是在 class 中声明的x
。所以要初始化一个x
你需要指定范围的实例:
Instead of
代替
x myX(10);
myX.XEnumType = Linear;
try
尝试
x myX(x::LINEAR);
By specifying x::
you provide the scope of the constant.
通过指定x::
您提供常量的范围。