“New” operator works in extern “C”

百般思念 提交于 2021-01-27 13:08:47

问题


Im using a C++ DLL for my C# project, DLL has a class inside which is created and destroyed by outer functions such as:

 class myClass
 {
   int N;
   public:
        //some elements and some functions

        myClass(int n)
        {
            N=n;
        }
 };

 __declspec(dllexport) myClass * builderF(int n)
 {

      return new myClass(n);

 }

 __declspec(dllexport) void destroyerF(myClass * c)
 {

      delete c;

 }

and these are in extern "C" {} body.

How does the compiler let me use C++ features is "C" space? Isnt it for only C code? This is tested and works(Ive started making an opencl wrapper for C#). I was using only pure C codes before.


回答1:


extern "C" doesn't change the compiler into a C compiler. It only says that any functions (or pointers to functions) will use the C conventions in their interface. So you can still do anything you could do in C++ in the functions; it's only things like name mangling and calling conventions which will be affected.



来源:https://stackoverflow.com/questions/17969491/new-operator-works-in-extern-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!