简体   繁体   中英

C, Assigning enum to variable using full name

I'm trying to assign an enum value to an enum variable with its "Full" name in c. Is this possible?:

 enum EnumActions{
     SHIFT,
     REDUCE,
     ACCEPT,
     GOTO ,
     ERROR_A 
    };

int main(){
    enum EnumActions __actionType;
    __actionType = EnumActions::SHIFT; //this is giving an error
    __actionType = SHIFT;//this works fine
}

any ideas how I can get the first one working?

The answer is NO, in C it's not possible.

You could do it with a C++11 enum class or with C++ namespace 's:

If you want to make it easier to understand you can use a prefix:

enum EnumActions{
    ACTION_SHIFT,
    ACTION_REDUCE,
    ACTION_ACCEPT,
    ACTION_GOTO ,
    ACTION_ERROR_A 
};

//...

__actionType = ACTION_SHIFT;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM