Using C++ classes in C: How to properly write some sort of "delete/dispose" function?

סטנלי גרונן

I need to use a C++ class in C code.

With such a C++ class:

class MyClass 
{
public:
    void myFunction()
    {
        cout << "Value = " << m_value;
    }

private:
    int m_value;
};

I have the following wrapper:

typedef void CMyClass;

#ifdef __cplusplus
extern "C" {
#endif

CMyClass * new_MyClass();
void c_myFunction(const CMyClass * ptr);
void c_dispose( ??? ); // to delete/destruct

#ifdef __cplusplus
}
#endif

// implementation below:

CMyClass * new_CMyClass() 
{
    MyClass * ptr = new MyClass();
    return (CMyClass*)ptr;
}

void c_myFunction(const CMyClass * ptr) 
{
    MyClass * tmp = (MyClass*)ptr;
    tmp->myFunction();
}

void c_dispose( ??? )
{
    /* ??? */
}

I would like just to make sure I won't have any problems like memory leaks, zombie pointers, with my c_dispose function.

How to properly write the complete code for c_dispose(???) to delete the previously created (with new_CMyClass()) object instance?

Bence Kaulics

A possible solution would be to simply call the delete once you casted the C type to C++ class.

void c_dispose(CMyClass * ptr){
    MyClass * tmp = (MyClass*)ptr;  
    delete tmp;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

sort function not working properly (bubble sort) - c

From Dev

quick sort error in c++ using classes

From Dev

c# how to properly write into a .dbf (foxpro)

From Dev

How to write one to many classes c#

From Dev

How to write recursive function properly?

From Dev

Using abstract classes as argument for function in C++

From Dev

Using abstract classes as argument for function in C++

From Dev

How to sort a 2D array using the sort function in c++?

From Dev

How to sort a 2D array using the sort function in c++?

From Dev

C++ Using pointers for selection sort function

From Dev

C++ Classes not initialized properly

From Dev

How to properly receive string from a function in c

From Dev

how can i cast float to char to write on a file using write function in c++?

From Dev

How to write a generic function in c#?

From Dev

How would I write this C function in Rust?

From Dev

How to write lambda function with arguments? c++

From Dev

How to write to variables passed to the Variadic function in C

From Dev

How to use sort() in C++ with custom sort member function?

From Dev

How to write an easy to expand set of color classes in C++

From Dev

Using ftw() properly in c

From Dev

Sort Array function in C

From Dev

Write Function in C

From Dev

How to write to a JSON file using C#?

From Dev

how to write ls command in c using execvp

From Dev

Would like to know how these works (swap function and sort function in C)

From Dev

bubble sort on array of c structures not sorting properly

From Dev

Merge Sort Algorithm in C not working properly

From Dev

Merge Sort Not Properly Sorting (C++)

From Dev

Merge Sort Algorithm in C not working properly

Related Related

HotTag

Archive