gpt4 book ai didi

c++ - 是否可以在 DLL 中导出具有 C 链接的 C++ 成员方法?

转载 作者:行者123 更新时间:2023-11-30 18:40:02 26 4
gpt4 key购买 nike

在 cpp 文件中考虑这一点:

struct someStruct{
public:
extern "C" __declspec(dllexport) int sumup();
} someStruct;

extern "C" __declspec(dllexport) int someStruct::sumup()
{
return 0;
}

这不会编译:错误:字符串常量之前预期有不合格的 id是否无法通过 C 链接导出 C++ 成员方法?

最佳答案

首先,链接规范不适用于成员函数;通过 [dcl.link]/4:

[...] A linkage-specification shall occur only in namespace scope (3.3). [...]

但是标准中甚至有一个与您的问题在某种程度上相关的示例,在同一段落中:

[...] A C language linkage is ignored in determining the language linkage of the names of class members and the function type of class member functions. [Example:

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
}
};

end example]

关于c++ - 是否可以在 DLL 中导出具有 C 链接的 C++ 成员方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com