通过从C到C指针++,反之亦然 [英] passing pointers from C to C++ and vice versa

查看:130
本文介绍了通过从C到C指针++,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么秘诀人能给我说说传递指针结构,双打,功能,...从C程序一个C ++库,回来了?


解决方案

假设你在两个不同的库静态或动态编码这些(DLL文件在Windows平台的Linux和其他* nix的变种共享库)我是最大的担忧如下:


  1. 它们编译相同的编译器。而如果所有的C ++的出口与到C C风格的命名约定是必要的C ++ ++调用两个C ++模块之间的类的实例出口,这是没有必要的。这是必要的,因为编译器如何区别不同裂伤C ++的出口。


  2. 不要投一个C ++类作为C结构。它们不是下盖是相同的,即使字段的布局是一样的。 C ++类有一个v表,如果他们有任何虚拟成员;这款V表允许继承或基类的方法正确的话。


  3. 这是C到C或C ++到C ++,以及为C到C ++真实。保证使用相同的字节对齐的输出库。您只能通过阅读你的编译器或开发环境文档,确定这一点。


  4. 不要混合使用的malloc /免费新/删除。更具体地讲不与自由,反之亦然新的可用内存分配内存。许多编译器和操作系统进行内存管理不同,两者之间。


  5. 传递函数指针:只要它们暴露于/从C ++作为''的externC'这应该是罚款。 (我们会在需要引用对如何确定时,头部被编译为C或C ++保持这种在一个文件中,否则你就需要在每个项目相同的函数声明的两个单独的副本你的编译器的文档 - 我推荐第一批)


  6. 传双打:这是一个内置的C和C ++类型,应办理相同


  7. 如果您必须共享一个C的实例++与C函数对象,并从C code内就此采取行动,揭露了一系列其中要求对C相应的方法C-出口辅助函数++目的。纯C code无法正常通话的C ++对象的方法。




    伪code-ISH例子:
    // C ++类
    类Foo {
       上市:
           DOIT无效();
    };    //出口报关单帮手
    为externC无效call_doit(富* PFOO);
    为externC富* allocate_foo();
    为externCdeallocate_foo(富* PFOO);
    //执行
    无效call_doit(富* PFOO)
    {
        pFoo-> DOIT();
    }    富* allocate_foo()
    {
        返回新的Foo();
    }    deallocate_foo(富* PFOO)
    {
       删除PFOO;
    }    // C消费
    无效的主要()
    {
        富* PFOO = allocate_foo();
        call_doit(PFOO);
        dealocate_foo(PFOO);
    }

Is there any tips one can give me about passing pointers to structs, doubles, functions, ... from a C program to a C++ library and back?

解决方案

Assuming you're coding these in two different libraries static or dynamic (DLLs on windows shared libraries on Linux and other *nix variants) The biggest concerns I have are as follows:

  1. They are compiled with the same compiler. While this isn't necessary if all C++ exports are exported with a C-style naming convention it is necessary for C++ to C++ calls to class instances between the two C++ modules. This is necessary due to how different compilers mangle C++ exports differently.

  2. Do not cast a C++ class as a C struct. They aren't the same under the covers, even if the layout of fields are the same. C++ classes have a "v-table" if they have any virtual members; this v-table allows the proper calling of inherited or base class methods.

  3. This is true of C to C or C++ to C++ as well as C to C++. Ensure both use the same byte alignment for the output library. You can only determine this by reading your compiler or development environments documentation.

  4. Don't mix malloc/free with new/delete. More specifically don't allocate memory with new and free memory with "free" and vice versa. Many compilers and operating systems handle memory management differently between the two.

  5. Passing function pointers: So long as they are exposed to/from C++ as ''extern "C"'' this should be fine. (You'll either need to reference your compilers documentation on how to determine when a header is being compiled as C or C++ to maintain this in one file, or you will need two separate copies of the same function declaration in each project -- I recommend the first)

  6. Passing doubles: This is a built-in type in both C and C++ and should be handled the same.

  7. If you must share an instance of a C++ object with a C function, and act on it from within C code, expose a set of C-exported helper functions which call the appropriate methods on the C++ object. Pure C code cannot properly call methods on C++ objects.


    Pseudocode-ish Example:
    // C++ class
    class foo {
       public:
           void DoIt();
    };

    // export helper declarations
    extern "C" void call_doit(foo* pFoo);
    extern "C" foo* allocate_foo();
    extern "C" deallocate_foo(foo* pFoo);


    // implementation
    void call_doit(foo* pFoo)
    {
        pFoo->DoIt();
    }

    foo* allocate_foo()
    {
        return new foo();
    }

    deallocate_foo(foo* pFoo)
    {
       delete pFoo;
    }

    // c consumer
    void main()
    {
        foo* pFoo= allocate_foo();
        call_doit(pFoo);
        dealocate_foo(pFoo);
    }


这篇关于通过从C到C指针++,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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