Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"New" operator works in extern "C"

Tags:

c++

c#

extern

dll

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.

like image 295
huseyin tugrul buyukisik Avatar asked Feb 04 '26 01:02

huseyin tugrul buyukisik


1 Answers

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.

like image 193
James Kanze Avatar answered Feb 05 '26 13:02

James Kanze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!