passing an int to a function expecting an enumerated datatype

时间:2016-12-02 04:47:34

标签: c++ arduino

For an arduino project, I am trying to use a library which contains code like this.

typedef enum
    {
      NOT_SPECIFIED = 0,
      MALE    = 1,
      FEMALE    = 2
    } gender;

and there is a function

void findCustomer(const char* userName, gender g);

In my own code where I let users input these info through a web interface, I save the gender entry as an int. Later, I want call findCustomer but if I pass an int as the second argument it gives me an error. To solve this, what is the dataType I should use to save the customer input so I can pass it on to findCustomer

1 个答案:

答案 0 :(得分:2)

what is the dataType I should use to save the customer input so I can pass it on to findCustomer

You can use static_cast to cast your second argument from int to enum which should satisfy findCustomer prototype, something like this:

findCustomer( userName, static_cast<gender>(int_argument) )