I am using a 3rd party library, which returns a newly created object as a raw pointer. I store it in a unique_ptr
with a custom destructor to properly destroy the object by calling the library's custom destructor. However, this object will be used by the other objects owned by the same parent class. I wonder if it's an acceptable design to pass the third party object as a reference into the other objects. As they have the same parent, the life time of the 3rd party object can be beyond the other objects as long as it's declared before the other objects that will keep it as a reference. Is there any concerns in this idea, or a better approach? Here is an example:
class ChildClass
{
public:
ChildClass (ThirdPartyClass& third) : thridparty (third)
{
// do sth...
}
private:
ThirdPartyClass& thirdparty;
};
class ParentClass
{
public:
void addChild()
{
children.emplace_back (*thirdParty);
}
private:
std::unique_ptr<ThirdPartyClass> thirdParty;
std::vector<ChildClass> children;
};