C ++ / CLI:从非托管枚举转换为托管枚举 [英] C++/CLI : Casting from unmanaged enum to managed enum

查看:271
本文介绍了C ++ / CLI:从非托管枚举转换为托管枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从原生代码枚举转换为托管代码枚举包含相同的枚举值?在C ++ / CLI中使用C#方式有什么区别,例如(int)

What is the correct way of casting (in C++/CLI) from a native code enum to a managed code enum which contain the same enum values? Is there any difference with using the C# way of casting like for example (int) in C++/CLI.

推荐答案

假设您的原生代码是

enum shape_type_e
{
    stUNHANDLED     = 0,            //!< Unhandled shape data.
    stPOINT         = 1             //!< Point data.
    ...
};

且您的托管代码

public enum class ShapeType
{
    Unhandled   = 0,
    Point       = 1,
    ...
};

您可以使用

shape_type_e nativeST = stPOINT;
ShapeType managedST = static_cast<ShapeType>(nativeST);
Debug.Assert(managedST == ShapeType::Point);

我总是使用 static_cast

这篇关于C ++ / CLI:从非托管枚举转换为托管枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆