-1

Is possible to cast between C++ class and C structure? (the use case here is that I have two different projects one in C++ and the other is in C and I have an interface to connect both projects)

I have tried some examples where both (a C++ class and a C structure) have the same member functions and member variables and this worked fine. But once there is a missing member function/variable in any of them that does not work and casting doesn't give the right values for the member variables.

One thing I've noticed is that if I add a destructor to the C++ class this would work fine as far as the destructor is not defined virtual?

In C++:

class myClass{
public:
    myClass() {}
     ~myClass(){} // if I define this destructor as virtual the casting fails 
    int     var;
};

In C:

struct myStru {
    int    var;
};

Casting in C

myStru *struObj = (myStru*)malloc(sizeof( myStru));
struObj = (myStru*) classObj; // classObj is a void pointer that is passed to the `C` project and it was created in the `C++` project

EDIT: I found a good answer to my question in this article (see Accessing C++ Classes from C): https://www.oracle.com/technical-resources/articles/it-infrastructure/mixing-c-and-cplusplus.html

Moe
  • 382
  • 4
  • 10
  • Remove `= (myStru*)malloc(sizeof( myStru))`, or you will cause memory leak. – MikeCAT Nov 27 '20 at 14:52
  • 2
    The answer to your [previous question](https://stackoverflow.com/questions/65037779/cast-class-object-to-struct-object-c-c) already told you that you cannot do this (as it invokes *undefined behavior*), what makes this one different? – UnholySheep Nov 27 '20 at 14:53
  • Does this answer your question? https://stackoverflow.com/questions/65037779/cast-class-object-to-struct-object-c-c – 463035818_is_not_an_ai Nov 27 '20 at 14:55
  • @UnholySheep there are some answers here (e.g. @ Ted Lyngmo's answer) that show that is possible. I would keep both questions as the answers here are useful. Besides, this question is mainly about the destructor of the class. I'd agree that the two questions are relativly close but the answers here are more useful. – Moe Nov 30 '20 at 19:50
  • @idclev463035818 the answers here are more useful. I presented the question differently here hoping to get other answers which is the case now. Please see @ Ted Lyngmo's answer below which is helpful. Thanks! – Moe Nov 30 '20 at 19:51

2 Answers2

1

One way is to not cast at all but to let the C++ class inherit from the C struct.

Example:

#include <iostream>

// #include "the_c_header.h"

class myClass : public myStru {
public:
    myClass(int v) : myStru{v} {}

    virtual ~myClass() = default;
};

class der : public myClass {
public:
    using myClass::myClass;
    
private:
    int not_copied;
};

int main() {
    der x(123);

    myStru p = x;

    std::cout << p.var << '\n';
}

Now, p is a copy of the myStru part of x.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

You can't cast directly as it causes UB. If you had the connection interface as a .cpp file rather than a .c, then you could convert with a function. Something like:

myStru classToStruct(myClass& classObj) {
    myStru result;
    result.var = classObj.var;
    return result;
}

It's not exactly elegant but it would work. It might be worth looking into restructuring the project, for example inheriting the class from the struct as others have mentioned.

shon-r
  • 139
  • 6