E
EnsGabe
Suppose you have a class heirarchy as such:
class Base{
....
};
class Mid1 : public Base{
....
};
class Mid2 : public Base{
....
};
class Derived: public Mid1, Mid2{
};
Mid1, Mid2, and Base are ABC.
What is an effective way to manage the lifetime of this object?
Currently, I leak Derived (and anything similar to Derived) all over
the place by not reclaiming them at all. That works for now, in as
much as when I am done with them I am done with execution of my
program, but is not a solution that will work for the future.
My use at the moment is as such:
class ObjectHolder {
public:
....
void addMid1(Mid1 *p) { assert(p); mid1s.push_back(p); };
void addMid2(Mid2 *p) { assert(p); mid2s.push_back(p); };
private:
std::vector<Mid1*> mid1s;
std::vector<Mid2*> mid2s;
....
};
There will be many different classes that are equivalent to Derived
(in the sense that they are deriving from Mid1 and Mid2.) There will
also be classes that derive from one or the other of Mid1 and Mid2.
All of them will be registered exactly once in mid1s and/or mid2s,
whichever they are derived from. References to the objects will not
exist outside of ObjectHolder. Objects may contain references to
other objects; the directed graph that they form will be acyclic.
My first instinct was to delete every element of mid1s and mid2s in
the destructor for ObjectHolder, but that will not work when a Derived
has been inserted into both vectors. My next idea was to use
boost::shared_ptr and use that to automatically manage the lifetime of
the object, but there was nothing in the documentation that I saw that
led me to believe that one could use a shared_ptr across a class
heirarchy. Since the 'this' pointer for an object is different
depending on the type it is being used as, I'm (possibly erroneously)
assuming that a shared_ptr will mismanage the reference count for the
pointer since they are using different pointers. Am I wrong in my
understanding of the semantics of boost::shared_ptr?
(PS: It's obvious that what would solve my problem is a GC- I'm not
ruling it out at this point, but I am exploring my options)
class Base{
....
};
class Mid1 : public Base{
....
};
class Mid2 : public Base{
....
};
class Derived: public Mid1, Mid2{
};
Mid1, Mid2, and Base are ABC.
What is an effective way to manage the lifetime of this object?
Currently, I leak Derived (and anything similar to Derived) all over
the place by not reclaiming them at all. That works for now, in as
much as when I am done with them I am done with execution of my
program, but is not a solution that will work for the future.
My use at the moment is as such:
class ObjectHolder {
public:
....
void addMid1(Mid1 *p) { assert(p); mid1s.push_back(p); };
void addMid2(Mid2 *p) { assert(p); mid2s.push_back(p); };
private:
std::vector<Mid1*> mid1s;
std::vector<Mid2*> mid2s;
....
};
There will be many different classes that are equivalent to Derived
(in the sense that they are deriving from Mid1 and Mid2.) There will
also be classes that derive from one or the other of Mid1 and Mid2.
All of them will be registered exactly once in mid1s and/or mid2s,
whichever they are derived from. References to the objects will not
exist outside of ObjectHolder. Objects may contain references to
other objects; the directed graph that they form will be acyclic.
My first instinct was to delete every element of mid1s and mid2s in
the destructor for ObjectHolder, but that will not work when a Derived
has been inserted into both vectors. My next idea was to use
boost::shared_ptr and use that to automatically manage the lifetime of
the object, but there was nothing in the documentation that I saw that
led me to believe that one could use a shared_ptr across a class
heirarchy. Since the 'this' pointer for an object is different
depending on the type it is being used as, I'm (possibly erroneously)
assuming that a shared_ptr will mismanage the reference count for the
pointer since they are using different pointers. Am I wrong in my
understanding of the semantics of boost::shared_ptr?
(PS: It's obvious that what would solve my problem is a GC- I'm not
ruling it out at this point, but I am exploring my options)