0

次のような列挙型クラスがあります。

class ContentTypeEnum {

 public:

  // it might have more types
  enum Code { TEXT, XML, APPLICATION_JSON};

  static const char* to_c_str(unsigned);

};

今のところ、コードでこのように使用していました。

ContentTypeEnum::APPLICATION_JSON

問題文:-

これで文字列が与えられたので、その文字列を使用してから、上記の列挙型を反復して実際の ENUM 型を見つける必要があります。

以下は私のコードです:

cout<<"Given String: " << data_args->pp_args->ter_strings[0].c_str() << endl;
const char* test_str = data_args->pp_args->ter_strings[0].c_str();

test_strxmlまたはの場合XML、次のように設定する必要があります。

TestClass::SetContentType(ContentTypeEnum::XML)

しかし、test_strapplication_jsonまたはAPPLICATION_JSONの場合、次のように設定する必要があります。

TestClass::SetContentType(ContentTypeEnum::APPLICATION_JSON)

また、他の人についても同様です。以下は私の完全なコードです:

cout<<"Given String: " << data_args->pp_args->ter_strings[0].c_str() << endl;
char* test_str = data_args->pp_args->ter_strings[0].c_str();

// look up the exact ContentType from the enum using test_str string
// and then set it to below method.
TestClass::SetContentType(set_it_here_basis_on_string_test_str)

誰かが私の列挙型にない未知の文字列を渡している場合、デフォルトで次のように使用する必要がありますTestClass::SetContentType(ContentTypeEnum::APPLICATION_JSON)

文字列を指定して正確な列挙型を検索する正しい方法は何ですか?

4

1 に答える 1