据我所知,目前无法对 C++11 enum class
执行 typedef
。我想知道在封装类之外引用变量 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
,这非常冗长。我不想做的两件事是:
typedef
在SegmentActivityState
上。- 将
枚举类
ActivityStateType移到类
SegmentActivityState` 定义之外。
最佳答案
您的问题与枚举无关。您不能这样做,因为您正在尝试访问未定义类的成员。无论成员是什么,这都行不通。
将 typedef 放在类定义和这个 will be completely fine 之后.
关于枚举类的 c++ typedef/type 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29876368/