C++ 内部的枚举班级

发布于 2024-11-05 08:36:11 字数 572 浏览 0 评论 0原文

我正在尝试使用一个在类内部声明了枚举类型的类,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

池予 2024-11-12 08:36:11

首先,您需要在类中声明一个 XEnumType 类型的变量
然后,您可以使用范围的类名来访问实际的枚举值:x::LINEARx::DIPRABOLIC

class x{
 //Your other stuff

XEnumType myEnum;
};

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

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;
}
魔法唧唧 2024-11-12 08:36:11

第一:不要使用typedef。相反,将枚举的名称放在其头部

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

简而言之,就像您所做的那样,其行为大部分是相同的,但在神秘的极端情况下会有所不同。您使用的语法的行为与我上面仅在 C 中使用的语法非常不同。

第二:这只是定义了一个类型。但您想定义该枚举的对象。这样做:

XEnumType e;

总而言之:

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;
}

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;
}
燕归巢 2024-11-12 08:36:11
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;
};

用法:

x myX(10);
myX.my_enum = 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;
爱本泡沫多脆弱 2024-11-12 08:36:11

您声明了一个新类型: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;
};

然后你可以这样访问它:

x foo(10);
foo.type = 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;
污味仙女 2024-11-12 08:36:11

该行

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

定义了一个名为 XEnumType类型,实际上这无论如何都是多余的 - 更喜欢这样的内容:

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

现在您需要在您的类中

XEnumType _eType;

在构造函数中定义此类型的成员,然后你可以初始化为任何

x::x(int ) : _eType(x::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) {}
何以笙箫默 2024-11-12 08:36:11

让我首先假设一些前提条件:

  • x来自第三方库,因此无法更改。
  • x在枚举的帮助下定义了一些整数常量。
  • x 应该使用常量LINEARDIPABOLIC 之一进行初始化。

您的问题是这些常量值是在类 x 中声明的。因此,要初始化 x 的实例,您需要指定范围:

而不是

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

尝试

x myX(x::LINEAR);

通过指定 x:: 您提供常量的范围。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文