是否有可能导出与C链接一个C ++成员方法在DLL? [英] Is it possible to export a C++ member method with C linkage in a DLL?

查看:136
本文介绍了是否有可能导出与C链接一个C ++成员方法在DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个CPP文件试想一下:

Consider this in a cpp file:

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

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

这不会编译:错误:字符串常量之前的预期不合格-ID
难道没有可以导出一个C ++成员方法与C链接?

This does not compile: error: expected unqualified-id before string constant Is it no possible to export a C++ member method with C linkage?

推荐答案

首先,联动规范并不适用于成员函数;通过[dcl.link / 4:

First off, linkage specifications don't apply to member functions; by [dcl.link]/4:

[...]一个联动规范,应仅在命名空间范围(3.3)发生。 [...]

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

但是,在涉及到你的问题有点,在同一段标准甚至是一个例子:

But there's even an example in the Standard that relates to your question somewhat, in the same paragraph:

[...] C语言的联动确定类成员和类成员函数的函数类型的名称的语言联动忽略。 [示例:

[...] 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
  }
};


  
  

- 结束例如的]

这篇关于是否有可能导出与C链接一个C ++成员方法在DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆