C++/CLI : Casting from unmanaged enum to managed enum

前端 未结 2 1361
我寻月下人不归
我寻月下人不归 2020-12-25 12:49

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

2条回答
  •  甜味超标
    2020-12-25 13:37

    Assuming your native code is

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

    and your managed code is

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

    You can cast from the native to the managed using

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

    I always use static_cast, not the C# way of casting.

提交回复
热议问题