作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用一个在类中声明了枚举类型的类,如下所示:
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::LINEAR
或 x::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/
我是一名优秀的程序员,十分优秀!