I am trying to understand C++/CLI so I can create Wrapper classes for C++ code. My problem is that I have a class which stores a pointer to a parent object of that class, therefore I need to pass it into the class.
Below is an example, but the full class has more functions and stores extra data.
class A
{
private:
A* parent;
public:
A(A* Parent)
{
parent = Parent;
}
~A()
{
delete parent;
}
A* GetParent()
{
return parent;
}
}
My current idea is to have a non-public constructor so that you can construct a managed class with the unmanaged one without it being accessible outside the class.
public ref class ManagedA
{
A* unmanaged;
ManagedA(A* unmanaged)
{
this->unmanaged = unmanaged;
}
public:
ManagedA(ManagedA^ Parent)
{
unmanaged = new A(Parent->unmanaged);
}
~ManagedA()
{
delete unmanaged;
unmanaged = NULL;
}
ManagedA^ GetParent()
{
return gcnew ManagedA(unmanaged->GetParent());
}
}
While this works for functions inside the class, I still have issues if I want to create an object or if I have a function that needs the unmanaged class to be passed in.
Is there a way I can work around this?