How should a C ++ class be used when its memory is reserved from C malloc?
I use the C library (lua), and I need to open the C ++ class for it, in this case, so that the garbage collects these reserved spaces, lua makes a memory reservation.
Below is a simpler scenario:
#include <string>
class Clase{
private:
std::string valor;
public:
Clase(){}
Clase(const std::string & valor) : valor(valor){}
const std::string & get() const { return this->valor; }
void set(const std::string & valor){ this->valor = valor;}
~Clase(){}
};
typedef struct
{
Clase cls;
}Estructura;
int main(int argc, char ** argv)
{
Estructura * est = (Estructura *) malloc(sizeof(Estructura));
est->cls.set("Hola");
Clase myCls;
est->cls = myCls;
return 0;
}
I understand and verified that with malloc the class constructor is not called; which was expected, and therefore the copy (assign) statement cannot be called with an invalid instance (string inside the class). I suspect that the second attempt failed at the same point when copying a string inside an instance of the class.
So:
- Is it possible to correctly initialize an instance of a class in which memory is reserved by malloc?
- What other reservations exist ?, vtables?
- malloc ? ( , Clase , ? [ , ))
Clase Estructura, , ?
, , lua ?, __gc - ?