H
Howard
Hi,
I recently had a problem where I decided to store objects in a vector.
(Previously, I had always stored pointers in vectors). Well, naturally,
when storing an object in a vector, using push_back, the object I had in
hand was getting copied (twice, in fact). That led to a problem, in that my
object contained a "handle" to another object, and when the object being
pushed went out of scope and was destroyed, the referenced object was also
destroyed, making the copy inside the vector refer to a now non-existent
object. Changing my vector to store pointers instead of objects resolved
that problem. But I'm wondering if that's the "best" way to handle the
problem.
I see a few options.
One was to create a copy constructor for my object, which created a new
object and a new sub-object as well. But that had two problems. One, it
meant I was using extra resources and time making that extra sub-object that
I didn't really need. Two, it meant that to get the "handle" that was
stored in the object I pushed into the vector (which I wanted to return from
the function that was doing the push_back), I had to get it by extracting
the object back out of the vector and querying it, which seemed a waste.
Another idea was to make my copy-constructor take ownership of the
handle from the passed object. That meant that the copy constructor had to
change the contents of the object passed to it (to set the handle to NULL),
but... I had declared the parameter as a const reference! I didn't feel
that changing that from a const reference was a great idea (although I could
be wrong...it's just that whenever I make a copy constructor, I always make
the parameter a const reference).
And lastly, I could store pointers in the vector (and write code to
iterate of the vector to destroy the objects when I was done with the list).
That's what I chose to do eventually. But I avoided it at first because I
have no need for polymorphism, so storing pointers wasn't really needed,
except to resolve this ownership issue.
Which of the above methods might be considered "better"? (Or is there
another paradigm for this kind of work with containers? Perhaps using some
kind of reference-counted pointer instead of the bare "handle" inside my
object?)
Thanks,
Howard
I recently had a problem where I decided to store objects in a vector.
(Previously, I had always stored pointers in vectors). Well, naturally,
when storing an object in a vector, using push_back, the object I had in
hand was getting copied (twice, in fact). That led to a problem, in that my
object contained a "handle" to another object, and when the object being
pushed went out of scope and was destroyed, the referenced object was also
destroyed, making the copy inside the vector refer to a now non-existent
object. Changing my vector to store pointers instead of objects resolved
that problem. But I'm wondering if that's the "best" way to handle the
problem.
I see a few options.
One was to create a copy constructor for my object, which created a new
object and a new sub-object as well. But that had two problems. One, it
meant I was using extra resources and time making that extra sub-object that
I didn't really need. Two, it meant that to get the "handle" that was
stored in the object I pushed into the vector (which I wanted to return from
the function that was doing the push_back), I had to get it by extracting
the object back out of the vector and querying it, which seemed a waste.
Another idea was to make my copy-constructor take ownership of the
handle from the passed object. That meant that the copy constructor had to
change the contents of the object passed to it (to set the handle to NULL),
but... I had declared the parameter as a const reference! I didn't feel
that changing that from a const reference was a great idea (although I could
be wrong...it's just that whenever I make a copy constructor, I always make
the parameter a const reference).
And lastly, I could store pointers in the vector (and write code to
iterate of the vector to destroy the objects when I was done with the list).
That's what I chose to do eventually. But I avoided it at first because I
have no need for polymorphism, so storing pointers wasn't really needed,
except to resolve this ownership issue.
Which of the above methods might be considered "better"? (Or is there
another paradigm for this kind of work with containers? Perhaps using some
kind of reference-counted pointer instead of the bare "handle" inside my
object?)
Thanks,
Howard