switch, pointer to class containing enum

Hi mucking around with storing enums in a struct/class and trying to access the enum values through a pointer to the containing struct

the below code works and does what it is supposed to

class FOO
{
    private:


    public:


    int num = 0;
    struct st
    {
        enum fooz{ONE, TWO}foo_st;
        int one;
        int two;
    }St;

}foo;

const struct State 
{
    enum asd {qwe = 10, rty};

}*c;


void class_t()
{
    //foo.St.one = St.ONE;
    foo.num = foo.St.one;

    Serial.println(foo.St.one);
    Serial.println(foo.num);

    int rtger = c->asd::qwe;
    Serial.print("c->asd::qwe:");           Serial.println(c->asd::qwe);
    switch (rtger)
    {
        case c->asd::qwe :
            Serial.print("in case:");           Serial.println(rtger);
        break;
        default:

        break;
    }
}

i have another program im trying to do the same thing in but its coming up with errors saying the '.' and '->' arent allowed in the case statement.

is the above code a valid way of getting to the enum values, or did the code just get lucky

mucking around with storing enums in a struct/class and trying to call the enum values through a pointer to the containing struct

That doesn't make sense. You do not CALL values. You CALL functions. You ACCESS values.

is the above code a valid way of getting to the enum values

It is, but it doesn't make sense to have the enum a member of a struct that is a member of a class.

Why didn't you post the code that you are having problems with?

Why didn't you post the code that you are having problems with?

  • because its spread across multiple

this is the example i was meaning to have a look at

const struct State
{
    enum asd {qwe = 10, rty};

}*c;

is this switch statement valid

switch (rtger)
    {
        case c->asd::qwe :
            Serial.print("in case:");           Serial.println(rtger);
        break;
        default:

        break;
    }

the first case???

here is some of the other code

class Enum_State
{

    enum Shp_Style { CIRCLE, SQUARE, RECT, SLOT };  // shape styles to chooses from 
    enum Kerf_Off { CUT_INT = 0, CUT_EXT = 1 };             // internal or external kerf offsets    
    enum Line_Style { LINE, ARC };                  // line styles to process
    enum Tool_Trav { TRAVEL, CUT };                 // tool traveling options
    enum Init_St { STOP, ACCEL, DECEL, RUN };       // initial section speed state
    enum End_St { DWELL, NONE, PEIRCE, HOME };      // end of section speed state
    enum Stpr_Dir { FOWARD, REVERSE, NO_MOVE };     // stepper lead screw direction states
    enum Shp_Cut_Dir { CW, CCW };                   // shape direction of cut travel    
    // Mck ids, these values are used to be written directly into registers only or
    // bit shifted to get actual scaled number
    enum pwm_clk {  
        PWM_MCK         = 0,
        PWM_MCK_2       = 1,
        PWM_MCK_4       = 2,
        PWM_MCK_8       = 3,
        PWM_MCK_16      = 4,
        PWM_MCK_32      = 5,
        PWM_MCK_64      = 6,
        PWM_MCK_128     = 7,
        PWM_MCK_256     = 8,
        PWM_MCK_512     = 9,
        PWM_MCK_1024    = 10
    };
    // Mck ids, these values are used to be written directly into registers only    
    enum stp_res {  
        STP_M_RES       = 0,
        STP_M_RES_2     = 1,
        STP_M_RES_4     = 2,
        STP_M_RES_8     = 3,
        STP_M_RES_16    = 4,
        STP_M_RES_32    = 5
    };  

}extern state;

and the switch statements im having prouble with

void Shape::process_offsets() 
{   // change dimensions(cut lines) to take care of kerf width
    float offset = 0.0;
    // choose offset direction
    switch(Imported.cut_offset_dir) 
    {
        case state.Kerf_Off::CUT_INT : offset = -(Imported.cut_offset * 2); break;
        case state.Kerf_Off::CUT_EXT : offset = (Imported.cut_offset * 2); break;
    }

    switch (Imported.shape) 
    {   // apply offsets dependent on shape (doesnt really matter what shape)
    case (state->Shp_Style::CIRCLE) :
        Imported.diameter_mm    += offset;
        break;
    case (state->Shp_Style::SQUARE) :
        Imported.width          += offset;
        Imported.height         = Imported.width;
        Imported.diameter_mm    += offset;
        break;
    case (state->Shp_Style::RECT) :
        Imported.width          += offset;
        Imported.height         += offset;
        Imported.diameter_mm    += offset;
        break;
    case (state->Shp_Style::SLOT) :
        Imported.diameter_mm    += offset;
        Imported.width          += offset;
        break;
    }
}

and the type of error im getting

Create_Shape.cpp: 45:26: error: '->' cannot appear in a constant-expression
   SQUARE)

Create_Shape.cpp: 50:8: error: 'state' cannot appear in a constant-expression
   RECT)

I do not like your style of defining structs and creating instances of them in one step.

Defining the struct/class is something that should happen in a header file. Defining the instances of the structs or classes is something that should happen in a source file.

state, as you have defined it now, is an object of type Enum_State. It is not a pointer, so using the pointer dereference operator, ->, is not appropriate. That is the first issue you need to resolve.

I can not see any value in putting all the enums in a class. The class has no functionality. A namespace MIGHT be appropriate, but a class is not.

cheers paul.

const struct State 
{
    enum asd {qwe = 10, rty};

}*c;

who new i always thought that was a declaration

namespace works but not as i would expect

namespace State
{
    enum class Shp_Style { CIRCLE, SQUARE, RECT, SLOT };    // shape styles to chooses from 
};

and this is how it is working

USData.shape = State::RECT;

where as i thought you would access the enum like so

USData.shape = State::Shp_Style.RECT;

but im still getting the stupid '.' cant have the dot in a constant expression of the switch statement.

i read the standard and it says an enum class gets promoted to int type so it should work in a switch statement

this works well as far as readability goes, might stick with this approach, thanks for the namespace reminder, had all but forgotten about them

namespace Kerf_Offset {	// internal or external kerf offsets
	enum { internal, external };
};// end of Kerf_Offset

namespace Shp_Style {	// shape styles to chooses from
	enum { circle, square, rect, slot };
};// end of Shp_Style

namespace Line_Style {	// line styles to process
	enum { LINE, ARC };
};// end of Line_Style

I would put all the enums in one namespace. I would given them all names. Anonymous enums are hard to use.

namespace ENUMS
{
   enum Kerf_Offset {internal, external };
   enum Shp_Style { circle, square, rect, slot };
   // etc.
};

cheers what makes you say unnamed enums are harder to use than named versions