簡體   English   中英

是否可以在C ++和C#之間共享“枚舉類”?

[英]Is it possible to share an “enum class” between C++ and C#?

我指的是上一個問題: 是否可以在C#和非托管C ++之間共享一個枚舉聲明?

我想做類似的事情,但對於枚舉類而不是枚舉:

C ++代碼:

enum class MyEnum { ONE, TWO, THREE, ... }

C#代碼:

public enum {ONE, TWO, THREE, ...}

有一些預處理器魔術可以做到這一點嗎? 我似乎無法在代碼的C#部分中刪除“ class”關鍵字。

例如:在MyEnum.cs中

#if __LINE__        // if C++
#define public      // C++ code: removes public keyword for C++
#else
#define class       // does not work: unable to remove the class keyword for C#
#endif



enum class MyEnum { ONE, TWO, THREE, ... }


#if __LINE__
#undef public // ok: remove public = nothing definition in C++
#else
#undef class // does not work: #undef must be at the top of the file in C#
#endif

與C ++預處理程序相比,C#預處理程序的局限性更大。 您可以在源文件中保留C#語法,並使用C ++預處理程序技巧使其變為有效的C ++代碼。 像這樣:

// enum.cs
public enum MyEnum : int { ONE, TWO, THREE };

並在C ++中使用:

// enum-cpp.h
#define public
#define enum enum struct

#include "enum.cs"

#undef enum
#undef public

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM