在cpp文件中考虑以下问题:
struct someStruct{
public:
extern "C" __declspec(dllexport) int sumup();
} someStruct;
extern "C" __declspec(dllexport) int someStruct::sumup()
{
return 0;
}
无法编译:
error: expected unqualified-id before string constant
是否可以导出具有C链接的C ++成员方法?
最佳答案
首先,链接规范不适用于成员函数。通过[dcl.link] / 4:
[...]链接规范应仅在命名空间范围(3.3)中出现。 [...]
但是在同一段中,甚至在标准中甚至有一个示例与您的问题有些相关:
在确定类成员名称和类成员函数的函数类型的语言链接时,将忽略C语言链接。 [例:
extern "C" typedef void FUNC_c();
class C {
void mf1(FUNC_c*); // the name of the function mf1 and the member
// function’s type have C ++ language linkage; the
// parameter has type pointer to C function
FUNC_c mf2; // the name of the function mf2 and the member
// function’s type have C ++ language linkage
static FUNC_c* q; // the name of the data member q has C ++ language
// linkage and the data member’s type is pointer to
// C function
};
extern "C" {
class X {
void mf(); // the name of the function mf and the member
// function’s type have C ++ language linkage
void mf2(void(*)()); // the name of the function mf2 has C ++ language
// linkage; the parameter has type pointer to
// C function
}
};
—结束示例]