0

In a library I have come across a weird construction which serves as enum:

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;

The members are not initialized anywhere but even though, RC_MODE_ERROR equals 0, RELEASE_CONTROL_SUCCESS equals 1 and so on. I know because I have logged it with printf. I haven't seen anything like it so far. Why does it even work (I thought values will be initialized by random data by default, or 0)? Is there any add value from this over standard enum?

What can I try next?

13
  • 3
    @EdChum but this is not declared as an enum. These are members of a struct Commented Sep 7, 2017 at 12:24
  • 2
    Those values have to be defined somewhere, somehow. Keep digging! Commented Sep 7, 2017 at 12:25
  • 2
    They look like macros they are probably macro defined somewhere, is there some #define ... somewhere Commented Sep 7, 2017 at 12:26
  • 1
    Are you sure there isn't a cpp file that doesn't set these static members? What you are seeing should not happen unless it is coded to do so. Commented Sep 7, 2017 at 12:26
  • 2
    @EdChum it's OK, I think we all do that sometimes Commented Sep 7, 2017 at 12:27

2 Answers 2

7

To begin with, this is not an enum, this is a struct. These are different concepts, but I think you knew, just got confused by the usage here.

It is not expected for the members of a struct to be assigned to these values (as for example would happen with an enum).

I am fairly sure that these members get initialized somewhere in your code, or they are Macros, thus are defined somewhere.


After searching Github, they are initialized, like this:

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 dji_error.cpp.

Sign up to request clarification or add additional context in comments.

4 Comments

@Bremen You need to look here
@NathanOliver and Basya I was 30 secs faster =)
@gsamaras: you are fast. beat me to it!
-1

Static members needs to be separately defined.

Ex:

// 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

2 Comments

That's not true for const integer.
yes it is, for a const static without initializer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.