gpt4 book ai didi

casting - 将 int 转换为枚举

转载 作者:行者123 更新时间:2023-12-04 06:12:23 25 4
gpt4 key购买 nike

我正在尝试在 C++/CLI 帮助器类中使用通用枚举类型,如果转换不起作用,我希望它默认为 0。我的问题是 result = (T)0;不起作用。有没有解决的办法?

Error 1 error C2440: 'type cast' : cannot convert from 'int' to 'T'


generic <typename T> where T: value class, Enum
static void SetEnumPropertyValue(String^ value, [Out] T %result)
{
if (String::IsNullOrEmpty(value))
false;
if (!Enum::TryParse<T>(value, true, result))
{
result = (T)0;
}
}

最佳答案

要么使用:

   result = (T)Enum::ToObject(T::typeid, 0);

或者稍微“丑一点”:
   result = (T)(Object^)0;

关于casting - 将 int 转换为枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7637817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com