1

据我所知,目前无法执行typedefC++11 enum 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
}

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

  1. typedefSegmentActivityState
  2. 移动enum classActivityStateType outside of the classSegmentActivityState` 定义。
4

1 回答 1

2

您的问题与枚举无关。你不能这样做,因为你试图访问一个未定义类的成员。无论成员是什么,这都不会起作用。

将 typedef 放在类定义之后,这样就完全没问题了。

于 2015-04-26T10:19:54.957 回答