gpt4 book ai didi

c++ - C++ 类中的枚举

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:42 24 4
gpt4 key购买 nike

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

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;

显然这行不通。我该怎么做?

最佳答案

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

class x{
//Your other stuff

XEnumType myEnum;
};

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

关于c++ - C++ 类中的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5913359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com