[英]Peculiar enum in c++
在圖書館,我遇到了一個奇怪的構造,它可以作為枚舉:
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
有沒有附加值? 我將不勝感激。
首先,這不是一個枚舉,而是一個結構。 這些是不同的概念 ,但是我想您知道,只是對這里的用法感到困惑。
不應將結構的成員分配給這些值(例如,對於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;
靜態成員需要單獨定義。
例如:
// 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.