C++ 内部的枚举班级
我正在尝试使用一个在类内部声明了枚举类型的类,如下所示:
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;
};
我需要声明此类的实例并初始化枚举类型。我来自 C#,通常看到枚举是在类外部声明的,而不是像这里那样在类内部声明的。如何初始化枚举类型。例如,我想做这样的事情:
x myX(10);
myX.XEnumType = Linear;
显然这是行不通的。我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,您需要在类中声明一个
XEnumType
类型的变量然后,您可以使用范围的类名来访问实际的枚举值:
x::LINEAR
或x::DIPRABOLIC
First you need to declare a variable that is of the type
XEnumType
within your classThen you can access the actual enumeration values using the class name for scope:
x::LINEAR
orx::DIPARABOLIC
第一:不要使用
typedef
。相反,将枚举的名称放在其头部简而言之,就像您所做的那样,其行为大部分是相同的,但在神秘的极端情况下会有所不同。您使用的语法的行为与我上面仅在 C 中使用的语法非常不同。
第二:这只是定义了一个类型。但您想定义该枚举的对象。这样做:
总而言之:
First: Don't use
typedef
. Instead, put the name of the enumeration in its headIn 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:
In summary:
用法:
Usage:
您声明了一个新类型:
XEnumType
。您必须在x
类中创建该类型的字段。。
例如:
然后你可以这样访问它:
You declared a new type :
XEnumType
. You have to create a field of that type insidex
class..
For example:
Then you can access to it that way:
该行
定义了一个名为
XEnumType
的类型,实际上这无论如何都是多余的 - 更喜欢这样的内容:现在您需要在您的类中
在构造函数中定义此类型的成员,然后你可以初始化为任何
the line
defines a type called
XEnumType
, actually this is redundant anyway - prefer something like:Now you need to define a member of this type in your class
In your constructor, then you can initialize to whatever
让我首先假设一些前提条件:
x
来自第三方库,因此无法更改。x
在枚举的帮助下定义了一些整数常量。x
应该使用常量LINEAR
或DIPABOLIC
之一进行初始化。您的问题是这些常量值是在类
x
中声明的。因此,要初始化x
的实例,您需要指定范围:而不是
尝试
通过指定
x::
您提供常量的范围。Let me first assume some preconditions:
x
is from a third-party library and thus cannot be changed.x
defines some integer constants with the help of an enum.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 ofx
you need to specify the scope:Instead of
try
By specifying
x::
you provide the scope of the constant.