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

标签 c++ c++11 enums typedef enum-class

据我所知,目前无法对 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,这非常冗长。我不想做的两件事是:

  1. typedefSegmentActivityState 上。
  2. 枚举类ActivityStateType移到类SegmentActivityState` 定义之外。

最佳答案

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

将 typedef 放在类定义和这个 will be completely fine 之后.

关于枚举类的 c++ typedef/type 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29876368/

相关文章:

c++ - 访问作为 vector 对的 map 值 C++

c++ - 如何从 C++/Ncurses 中获得更多颜色

c++ - 最后一次使用可 move 对象时,编译器会自动使用 move 语义吗?

java - 如何将一些或多个对象附加到枚举?

PowerShell 不能使用匹配的枚举类型?

c++ - 如何使用模板选择类(class)成员

c++ - 在 Visual Studio 中调试时,我可以将一些 C/C++ 结构内容保存到磁盘吗?

c++ - 如何减少 C++ 中多重集中元素的数量?

linux - 如何在c++中不按Enter键读取字符

Java枚举类