> I have a class containing a simple pointer. I have a vector containing
> several of the aforementioned classes. Because of the pointer, this
[quoted text clipped - 7 lines]
> the time, but depends on the vector size or whether I am running the
> same code several times in a row within the program.
> > I have a class containing a simple pointer. I have a vector containing
> > several of the aforementioned classes. Because of the pointer, this
[quoted text clipped - 15 lines]
> things can happen in a case like yours. That's the same reason why you
> can't store STL auto_ptr objects in standard containers, too.
My mistake for being unclear. Pointing to the new object means creating
a new heap object filled using a deep copy, e.g.,
A::A(A const& source) {
if(pointer_) {
delete pointer_;
}
pointer_ = new B(*source.pointer_);
return;
}
Ed
Iforget - 27 Oct 2005 23:07 GMT
> > > I have a class containing a simple pointer. I have a vector containing
> > > several of the aforementioned classes. Because of the pointer, this
[quoted text clipped - 28 lines]
>
> Ed
oops, should be
A::A(A const& source) {
if(pointer_) {
delete pointer_;
}
if(source.pointer_) {
pointer_ = new B(*source.pointer_);
}
else {
pointer_ = 0;
}
return;
}
Ed
Allen Brunson - 28 Oct 2005 00:50 GMT
> My mistake for being unclear. Pointing to the new object means creating
> a new heap object filled using a deep copy, e.g.,
>
> [copy constructor snipped]
you still haven't answered the question posed to you, to wit: what does this
class' assignment operator look like? if you didn't write one, then the
compiler will generate one for you. since your object contains a pointer, the
compiler-generated assignment operator will do the wrong thing.
if your answer is "i don't use the assignment operator," you're incorrect,
because std::vector will call it on your behalf.
Iforget - 28 Oct 2005 15:15 GMT
> > My mistake for being unclear. Pointing to the new object means creating
> > a new heap object filled using a deep copy, e.g.,
[quoted text clipped - 8 lines]
> if your answer is "i don't use the assignment operator," you're incorrect,
> because std::vector will call it on your behalf.
The assignment operator is defined by me and it is basically the same as
the copy constructor. However, it checks for equality before doing
anything and returns *this with a reference.
Ed