在哪里定义枚举

use*_*113 7 c++

说我想创建一个卡类.并希望有卡的枚举

2,3,4,5,6,7,8,9,J,Q,K,A

哪个是定义它的最佳位置,头文件Card.h或Card.cpp?

Pab*_*ruz 5

这将是一个新的类型。因此,如果需要其他编译单元,我可能会将其放在头文件中。


Ger*_*ald 4

我会在你的卡类的定义中这样做:

class Card
{
public:
   typedef enum
   {
      Two = 0,
      Three,
      Four,
      Five,
      Six,
      Seven,
      Eight,
      Nine,
      Ten,
      Jack,
      Queen,
      King,
      Ace
   } Face;
};
Run Code Online (Sandbox Code Playgroud)

或 C++11:

class Card
{
public:
   enum Face: unsigned char
   { 
      Two = 0,
      Three,
      Four,
      Five,
      Six,
      Seven,
      Eight,
      Nine,
      Ten,
      Jack,
      Queen,
      King,
      Ace
   };
};
Run Code Online (Sandbox Code Playgroud)