c ++:使用“枚举类”的类中的枚举 [英] c++: enum inside of a class using "enum class"

查看:495
本文介绍了c ++:使用“枚举类”的类中的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类中写入枚举的正确方法是什么?我正在用c ++编写conway的生活代码游戏。所以我有一个模式类,其中包含有关不同类型模式的信息:

 类模式
{
public:
Patterns();
〜Patterns(void);

private:
枚举类PatternType
{
bloat,
块,
闪烁,
toad
} ;

PatternType pattern_;
};

我的目标不是在这里污染全局空间。那么写一个写在一个类中的枚举的方式,记住哎哟概念。如果没有,请提出一些更好的选择,它们比这种方法有什么好处。欢迎任何在写入枚举内容时注意事项的输入。



编辑:@Jay Miller根据他的输入,我已经尝试了他所有的三种方法

 枚举类PatternClassOut {point,circle,triangle}; 

类模式
{
public:
枚举PatternSimple {bloat,block,blinker,toad};
枚举类PatternClassIn {cat,mouse,dog};
//为所有枚举变量设置函数

private:
PatternClassOut out_;
PatternSimple simple_;
PatternClassIn in_;
};

我的发现(尝试了代码和谷歌之后):


  1. PatternSimple是在我的案例中写入枚举的最简单的方法;但是它类型安全性受损,即我可以将枚举值与任何其他数据类型进行比较,并且不会给出任何错误。

  2. PatternClassIn提供类型安全性;但是我必须在调用函数中编写更多的代码,以访问枚举值。

  3. PatternClassOut提供了两个世界中最好的。它提供了类型安全性,此外,从调用函数到访问枚举值的代码与PatternSimple的代码相同。


解决方案假设你希望这个在你的课堂之外可以使用,使得这个枚举公开是有道理的。作为一个枚举类,您将需要枚举名称以访问除了类名之外的值:

  class Patterns {
public:
enum class PatternType {...};
...
};
void makePattern(Patterns :: PatternType value){...};
makePattern(Patterns :: PatternType :: bloat);

因为它是一个枚举类,所以将它移出另一个类可能是完全有效的。全局命名空间的唯一添加将是枚举的名称。

 枚举类模式{
bloat,
...
};
void makePattern(Patterns value){...};
makePattern(Patterns :: bloat);

如果您有其他操作要与枚举分组,然后将其嵌套在类中说得通。在这种情况下,您可以决定使用枚举而不是枚举类

  class Patterns {
public:
enum PatternType {...};
std :: string toString()const {...};
...
private:
PatternType值;
};
...
...
void makePattern(Patterns :: PatternType value){...};
makePattern(Patterns :: bloat);


What would be the right way to write an enum inside a class? I am writing conway's game of life code in c++. So i have a patterns class which contains the info about different kind of patterns:

class Patterns
{
public:
    Patterns();
    ~Patterns(void);

private:
    enum class PatternType
    {
        bloat,
        block,
        blinker,
        toad
    };

    PatternType pattern_;
};

My goal is not to pollute the global space here. So is it the write way of writing the enum inside of a class keeping oops concepts in mind. If not, please suggest some better alternatives and what are their benefits over this method. Any inputs on keeping things in mind while writing enums inside of a class are welcome.

Edit: @Jay Miller Based on his inputs i have tried all of his three methods

enum class PatternClassOut{ point, circle, triangle };

class Patterns
{
public:
  enum PatternSimple{bloat, block, blinker, toad};
  enum class PatternClassIn{cat, mouse, dog};
  //get set functions for all the enum variables

private:
  PatternClassOut  out_;
  PatternSimple    simple_;
  PatternClassIn   in_;
};

My findings (After trying out the code & googling a bit):

  1. PatternSimple is the simplest way to write the enum in my case, inside of the class; but it compromises on type safety i.e i can compare the enum values with any other data type and it wont give any error.
  2. PatternClassIn provides type safety; but i have to write more code in the calling funcion, to access the enum values.
  3. PatternClassOut provides the best of both worlds. It provides type safety, moreover, the code from the calling function to access enum values is same as that of PatternSimple.

解决方案

Assuming you want this to be available outside your class, making the enum public makes sense. As an enum class, you will need the enum name to access the values in addition to the class name:

class Patterns {
public:
    enum class PatternType { ... };
...
};
void makePattern(Patterns::PatternType value) { ... };
makePattern(Patterns::PatternType::bloat);

Because it is an enum class, it may be perfectly valid to move it outside of another class. The only addition to the global namespace will be the name of the enum.

enum class Patterns {
   bloat,
   ...
};
void makePattern(Patterns value) { ... };
makePattern(Patterns::bloat);

If you have other operations you want to group with the enum then nesting it in a class as you have makes sense. You may decide, in that case, to just use enum rather than enum class:

class Patterns {
public:
    enum PatternType { ... };
    std::string toString() const { ... };
    ...
private:
    PatternType value;
};
...
...
void makePattern(Patterns::PatternType value) { ... };
makePattern(Patterns::bloat);

这篇关于c ++:使用“枚举类”的类中的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆