简体   繁体   English

是否有输入枚举类型变量而不是使用 static_cast 的替代方法

[英]Is there an alternative to input a enumerated type variable rather than using a static_cast

I was trying to input an enumerated type variable but i could not do it without a using a static_cast operation我试图输入一个枚举类型变量,但如果不使用 static_cast 操作我就无法输入

#include<iostream>
using namespace std;

enum month
{
    JAN,
    FEB,
    MAY,
    JUNE,
    OCTOBER,
    DECEMBER,
};
int main()
{
    month This;
    cin >> This; <<_______________________ Causes a compiler error
    system("pause");
    return 0;
}

One workaround is to read in an integer, and use a static_cast to force the compiler to put an integer value into an enumerated type一种解决方法是读入一个整数,并使用 static_cast 强制编译器将整数值放入枚举类型

{
int input_month;
cin >> input_month;

month This = static_cast<month>(input_month);<<_____________Works
}

So is there an alternative to inputting an enumerated type value那么是否有输入枚举类型值的替代方法

Here is an example how I would approach this (extending the answer of jacobi). 这是一个示例,我将如何处理(扩展jacobi的答案)。

#include <iostream>
#include <stdexcept>

enum month
{
    JAN,
    FEB,
    MAY,
    JUNE,
    OCTOBER,
    DECEMBER,
};

month int2month(int m)
{
    switch(m)
    {
        case 1: return month::JAN;
        case 2: return month::FEB;
        case 3: return month::MAY;
        case 6: return month::JUNE;
        case 10: return month::OCTOBER;
        case 12: return month::DECEMBER;
        default: throw std::invalid_argument("unknown month");
    }
}

std::istream& operator>>(std::istream& is, month& m)
{
    int tmp;
    if (is >> tmp)
        m = int2month(tmp);
    return is;
}

int main()
{
    month This;
    try{
       std::cin >> This;
    }
    catch(std::invalid_argument& e){
    // TODO: handle
    }
    return 0;
}

online example 在线示例

Notice that there are many more way how you could map 'int's to month. 注意,还有更多方法可以将“ int”映射到月份。 For example I think your code will give the same results as: 例如,我认为您的代码将获得与以下结果相同的结果:

month int2month(int m)
{
    switch(m)
    {
        case 0: return month::JAN;
        case 1: return month::FEB;
        case 2: return month::MAY;
        case 3: return month::JUNE;
        case 4: return month::OCTOBER;
        case 5: return month::DECEMBER;
        default: throw std::invalid_argument("unknown month");
    }
}

Out of scope for this question: 超出此问题的范围:

Also note that you could write an 'string2month' function. 另外请注意,您可以编写一个“ string2month”函数。 Then you could make 'tmp' an string. 然后,您可以将“ tmp”设置为字符串。 Depending on 'tmp' containg only digits, you could convert 'tmp' to an 'int' to convert this to an month, or try to convert 'tmp' to an 'month'. 根据仅包含“ tmp”的数字,您可以将“ tmp”转换为“ int”以将其转换为一个月,或尝试将“ tmp”转换为“ month”。 This would allow for inputs like JAN or January, depending on the implementation of 'string2month'. 这将允许像JAN或January这样的输入,具体取决于'string2month'的实现。

You would have to implement your own operator >> for the enum 'month' to do what you want. 您必须为枚举“月”实现自己的运算符>>才能执行所需的操作。

Example: 例:

std::istream& operator>>(std::istream& is, month& m)
{
    int tmp;
    if (is >> tmp)
        m = static_cast<month>(tmp);
    return is;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我们总是 static_cast&lt;&gt; 使用 void* 而不是任何指针类型是否有效? - Is it valid to use void* rather than any pointer type if we always static_cast<> it? 如何static_cast到变量类型 - How to static_cast to the type of variable 对于非指针类型,使用static_cast而不是C样式转换是否有任何优势? - Is there any advantage in using static_cast rather than C-style casting for non-pointer types? 是否有“安全”的 static_cast 替代方案? - Is there a "safe" static_cast alternative? 有没有使用static_cast的替代方法 <int> 每时每刻? - Is there an alternative to using static_cast<int> all the time? 为什么Qt使用reinterpret_cast而不是static_cast for void *? - Why does Qt use reinterpret_cast rather than static_cast for void*? 为什么在这里使用static_cast而不是dynamic_cast? - Why use static_cast rather than dynamic_cast in here? 用 static_cast&lt;&gt;() 声明变量时使用 auto 的目的是什么? - what is the purpose of using auto when declaring variable with static_cast<>()? 使用C ++样式转换从Void *转换为TYPE *:static_cast或reinterpret_cast - Cast from Void* to TYPE* using C++ style cast: static_cast or reinterpret_cast 使用static_cast然后使用dynamic_cast - Using static_cast and then dynamic_cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM