c++ typedef/type 替换枚举 class

c++ typedef/type substitution for enumeration class

据我所知,目前无法执行 C++11 enum classtypedef。我想知道在封装 class 之外引用变量 enum 时是否有任何其他方法可以减少它的名称长度。这是一个例子:

// I would like to do something along the lines of:
class SegmentActivityState;
using SegmentActivityType = SegmentActivityState::ActivityStateType;

// ...However, this results in the compile time error:
// 'SegmentActivityState' does not name a type. 

// Enumeration class definition
class SegmentActivityState
{
public:
    enum class ActivityStateType : Index
    {
        PreExecution = 0,   /**< Pre-execution state. */
        Execution = 1, /**< Execution state. */
        PostExecution = 2 /**< Post-execution state. */
    };

private:
    ActivityStateType ActivityState;
    /**< unique object identifier tag. */

public:
    // ... Methods that work on the ActivityState
}

最重要的问题是名称的长度,我必须用它来引用 SegmentActivityType 之外的 enum。例如,为了进行类型比较,我需要写 SegmentActivity.getState() == SegmentActivityState::ActivityStateType::PreExecution,这非常冗长。我不想做的两件事是:

  1. typedef SegmentActivityState
  2. 移动 enum classActivityStateTypeoutside of the classSegmentActivityState` 定义。

你的问题与枚举无关。您不能这样做,因为您正在尝试访问未定义 class 的成员。无论成员是什么,这都行不通。

将 typedef 放在 class 定义和这个 will be completely fine 之后。