Switching on structure passed as constructor parameter in C++

Viewed 24

I have a config header file with static configuration used in class 'MyClass'.

In class_config.h

typedef enum {
    SEQUENCE_1 = 5,
    SEQUENCE_2 = 8,
} Sequence;

typedef struct {
    int Position;
    Sequence FirstSequence;
    Sequence SecondSequence;   
} Map;

typedef struct {
    Map Mapping01;
    Map Mapping02;
} Config;

Config DefaultConfig {
    {
       1,
       SEQUENCE_1,
       SEQUENCE_2 
    },
    {
       2,
       SEQUENCE_2,
       SEQUENCE_2 
    },
};

In class.h


Class MyClass
{
public:
   MyClass(const Config &config);
private:
   void Foo(int position);
   const Config &internal_config;
}

In class.cpp


MyClass::MyClass(const Config &config) : internal_config(config)
{ }

void MyClass:Foo(int position)
{
  switch(position)
  {
     case internal_config.Mapping01.Position : // error : "this" cannot be used in a constant expression
         // Get : internal_config.Mapping01.SEQUENCE_1
         break;
     default:
         break;
  }
}


In my method 'Foo' I'd like to find if 'position' is equal to internal_config.Mapping01.Position or internal_config.Mapping02.Position for example to return the corresponding sequence mapped with. But I get the following error when compiling : "this" cannot be used in a constant expression

I know that switch case requires const expression at building time but this is what I though I was doing. Using if else pattern instead of the switch case one is quite bothering me.

Is there something I am missing ?

0 Answers
Related