枚举类的c++typedef/类型替换

c++ typedef/type substitution for enumeration class

本文关键字:类型 替换 c++typedef 枚举      更新时间:2023-10-16

据我所知,目前不可能执行C++11enum classtypedef。我想知道在封装类之外引用变量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. typedefSegmentActivityState
  2. 移动enum classActivityStateTypeoutside of the classSegmentActivityState`定义

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

把typedef放在类定义之后,这样就完全可以了。