c ++中的特殊枚举

时间:2017-09-07 12:21:29

标签: c++ struct enums initialization

在图书馆,我遇到了一个奇怪的结构,作为枚举:

typedef struct SetControl
{
  const static uint16_t RC_MODE_ERROR;
  const static uint16_t RELEASE_CONTROL_SUCCESS;
  const static uint16_t OBTAIN_CONTROL_SUCCESS;
  const static uint16_t OBTAIN_CONTROL_IN_PROGRESS;
  const static uint16_t RELEASE_CONTROL_IN_PROGRESS;
  const static uint16_t RC_NEED_MODE_F;
  const static uint16_t RC_NEED_MODE_P;
  const static uint16_t IOC_OBTAIN_CONTROL_ERROR;
} SetControl;

成员未在任何地方初始化,但即使RC_MODE_ERROR等于0,RELEASE_CONTROL_SUCCESS等于1,依此类推。我知道因为我用printf记录了它。到目前为止我还没有看到类似的东西。为什么它甚至可以工作(我认为值默认情况下会被随机数据初始化,或者0)?超过标准enum是否有任何增加值?我将非常感谢你的帮助。

2 个答案:

答案 0 :(得分:7)

首先,这是枚举,这是一个结构。这些是different concepts,但我想你知道,只是对这里的用法感到困惑。

期望将结构成员分配给这些值(例如,enum会发生这种情况。)

我很确定这些成员会在代码中的某个位置初始化,或者它们是宏,因此会在某处定义。

搜索Github后,它们会被初始化,如下所示:

const uint16_t DJI::OSDK::ErrorCode::ControlACK::SetControl::RC_MODE_ERROR = 0x0000;
const uint16_t DJI::OSDK::ErrorCode::ControlACK::SetControl::RELEASE_CONTROL_SUCCESS = 0x0001;
const uint16_t DJI::OSDK::ErrorCode::ControlACK::SetControl::OBTAIN_CONTROL_SUCCESS = 0x0002;
const uint16_t DJI::OSDK::ErrorCode::ControlACK::SetControl::OBTAIN_CONTROL_IN_PROGRESS = 0x0003;
const uint16_t DJI::OSDK::ErrorCode::ControlACK::SetControl::RELEASE_CONTROL_IN_PROGRESS = 0x0004;
const uint16_t DJI::OSDK::ErrorCode::ControlACK::SetControl::RC_NEED_MODE_F = 0x0006;
const uint16_t DJI::OSDK::ErrorCode::ControlACK::SetControl::RC_NEED_MODE_P = 0x0005;
const uint16_t DJI::OSDK::ErrorCode::ControlACK::SetControl::IOC_OBTAIN_CONTROL_ERROR = 0x00C9;
<{3>}中的

答案 1 :(得分:-1)

静态成员需要单独定义。

例如:

// in example.h
struct SetControl
{
    const static uint16_t RC_MODE_ERROR; // this is only a declaration
};

// in example.cpp
const uint16_t SetControl::RC_MODE_ERROR = 1; // this is the definition