I have a library which the structure is something like this:
class Subfeature {
};
class BigFeature {
Subfeature* subfeature;
// more properties/methods ...
public:
Subfeature* get_subfeature();
BigFeature();
~BigFeature(); // In this destructor, subfeature is deleted
};
I found out that this library is very messy (but it is so large for me to change something), and so I decided to make a wrapper class with following structure:
class BigFeatureWrapper {
class SubfeatureWrapper {
Subfeature* subfeature;
// more properties/methods etc.
public:
// This constructor calls get_subfeature method in BigFeature and set it in subfeature property
SubfeatureWrapper(BigFeature* bigfeature) {
this->subfeature = bigfeature->get_subfeature();
};
};
SubfeatureWrapper* subfeaturewrapper;
BigFeature* bigfeature;
public:
BigFeatureWrapper() {
this->bigfeature = new BigFeature();
this->subfeaturewrapper = new SubfeatureWrapper(this->bigfeature);
}
~BigFeatureWrapper() {
delete this->bigfeature;
// Should I define destructor for SubfeatureWrapper
// and write `delete this->subfeaturewrapper;` here?
}
};
Here, should I define and destruct subfeaturewrapper
to prevent undefined behaviour, memory leak etc.?
Because the destructor of BigFeature
deletes the pointer for subfeature
,
I'm wondering if I should remove Subfeature* subfeature
inside SubfeatureWrapper
also (or could it be redundant).
I'm completely new to pointers in C++C/C++, so detailed explanation will really help. Thank you!
P.S. I know the existence of smart pointers, but I can do nothing about it because my [external] library uses raw pointers :(