繁体   English   中英

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

[英]c++ typedef/type substitution for enumeration class

据我所知,目前typedef无法对C ++ 11 enum class进行typedef 我想知道在封装类之外引用变量enum时,是否还有其他方法可以减少变量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. SegmentActivityState typedef
  2. enum class ActivityStateType移到SegmentActivityState outside of the class定义之外。

您的问题与作为枚举无关。 您无法执行此操作,因为您正在尝试访问未定义类的成员。 无论成员是什么,这都将永远无法进行。

将typedef放在类定义之后, 就可以了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM