c++ - 如何使用 C 中的内部 c++ 类类型?
问题描述
我有一个MyClass
声明公共枚举类型的 C++ 类,MyEnum
我想在 C 文件中使用该枚举。我怎样才能做到这一点 ?
我试图在 C++ 文件中声明我的函数,然后将所有内容都设置为extern "C"
,但遗憾的是我使用了一些定义在其中的函数,big_hugly_include.h
并且这个头文件不喜欢被包含在内external "C"
(它给了我一个template with C linkage
错误)。
我不能(不想)改变这个包含,我需要它,因为它定义了my_function_from_big_include
. 我卡住了吗?
my_class_definition.h
:
class MyClass
{
public:
// I would like to keep it that way as it is mainly used in C++ files
typedef enum
{
MY_ENUM_0,
MY_ENUM_1,
MY_ENUM_2
} MyEnum;
};
尝试 1 :my_c_function_definition.c
:
#include "my_class_definition.h"
// I cannot remove this header
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
{
// I need to call this function with the enum from the C++ class
// This doesn't work (class name scope does not exist in C)
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
}
尝试 2 :my_c_function_definition.cpp
:
#include "my_class_definition.h"
extern "C"
{
// Error template with C linkage
#include "big_hugly_include.h"
// foo is called in other C files
void foo()
{
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
}
// end of extern "C"
}
编辑以回应@artcorpse
尝试 3 :my_c_function_definition.cpp
:
#include "my_class_definition.h"
// Error multiple definition of [...]
// Error undefined reference to [...]
#include "big_hugly_include.h"
extern "C"
{
// foo is called in other C files
void foo()
{
// That would be ideal
my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
}
// end of extern "C"
}
解决方案
我想在 C 文件中使用该枚举。我怎样才能做到这一点?
C++ 中的枚举概念源自 C,因此您唯一需要做的就是将此枚举与 C 不知道的纯 cpp API 隔离开来(请记住名称修饰,见下文)。
由于 C 在类/结构枚举中不知道你不能使用它们。您必须定义全局范围枚举或创建将映射 C++ 特定枚举的此类枚举。
因此,应在共享 API 所在的位置创建单独的头文件。做这样的事情:
// shared C, C++ header
#ifdef __cplusplus
extern "C"
{
#endif
enum YourMagicEnum {
YourMagicEnumAValue,
YourMagicEnumBValue,
YourMagicEnumCValue,
};
void someFunction(YourMagicEnum x);
#ifdef __cplusplus
} // extern "C"
#endif
现在extern "C"
只需要禁用名称修饰的函数(在 C++ 中,您可以进行函数重载,因此编译器生成包含参数类型信息的名称)。
在定义这样的函数时,它还应该extern "C"
在该定义的前面有 。
请记住,在该标题中只能放置 C 特定的特性和功能。
还要记住 VLA(可变长度数组)是 C 标准,但不是 C++ 标准(大多数编译器支持 C++ 的 VLA)。
有关详细信息,请参阅此页面。
推荐阅读
- angularjs - 为什么在遵循 Angular AD 官方指南时会出现此错误?
- html - 如何在输入类型“提交”中添加材质图标
- javascript - 如何在选中复选框时切换文本并在未选中任何内容时隐藏
- runnable - AUTOSAR:建议直接从Scheduler调用一个runnable?
- tornadofx - TornadoFX 向导:阻止移动到下一页,直到异步任务完成
- java - IE 浏览器在 Jenkins 运行期间停止
- php - 在页脚中首先处理 HTML
- angular-material - 角度材料如何定位 mat-sidenav 顶部或底部
- oracle - 第 1 行出现错误:ORA-00984:此处不允许列
- node.js - 在面临此错误的 Windows 机器上设置角度 4 的问题