简体   繁体   中英

How to pass an enum to a method without passing it as an integer

I Have an enum which contains 3 different values

enum
{
    inputValidation_Zipcode,
    inputValidation_String,
    inputValidation_Number
} InputValidation;

I am trying to pass one of these three enum values to a method, and have tried the following.

bool methodName(enum InputValidation inputenum)

bool methodName(InputValidation inputenum) 

and ofc

bool methodName(int inpoutenum) 

(All three called as methodName(InputValidation_Number) )

I know the last one will "work" but allows ALL integers as arguments. How can I Write a method to only accept the inputValidation values?

Your enum definition is wrong, it should be:

enum /*class*/ InputValidation
{
    inputValidation_Zipcode,
    inputValidation_String,
    inputValidation_Number
};

Then you might use:

bool methodName(InputValidation inputenum);

Give scoped enum a try by adding class as follows:

enum class InputValidation
{
    inputValidation_Zipcode,
    inputValidation_String,
    inputValidation_Number
};

For more information: https://en.cppreference.com/w/cpp/language/enum

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