Recently, I have been abstracting and cleaning up a project of mine to get it ready for new features. I came across a design problem when I realized that I wanted to manage pointers to a graphics context and a window as abstract classes. I have searched for answers but haven't found anything satisfying yet. I have a situation where I'm creating a window that extends the window class and the graphics context class like so...
class base_a
{
// base_a virtual methods
};
class base_b
{
// base_b virtual methods
};
class derived : public base_a, public base_b
{
//other methods
};
int main ()
{
derived* d = new derived();
// This is what I want to do
std::shared_ptr<base_a> ptr1 (d);
std::shared_ptr<base_b> ptr2 (d);
}
While this more or less works during the lifetime of the object, it becomes a problem when destructing since depending on the order of destruction you are potentially deleting empty memory. It's useful to have the pointers to both as I need access to the virtual functions and I would like to keep the graphics context and the window decoupled if possible. Is there a way to do this?
Edit: Changed unique_ptr
to shared_ptr
as the former was incorrect