Two points:
1 2 3 4 5
|
void T::append( T attach )
{
// make this point to nodes of attach
attach.clear();
}
|
When we use it:
1 2 3
|
T a;
T b;
a.append( b );
|
Lets pseudo-inline the function call:
1 2 3 4 5 6 7
|
T a;
T b;
// start append
T attach = b;
// make 'a' point to nodes of attach
attach.clear();
// end append
|
If 'b' had a node before calling append(), then 'b' points to that node, 'a' points to that node, and 'attach' points to that same node before calling clear(). Both 'a' and 'b' will point to deallocated space in the end.
It appears that you want to
move nodes from 'b' to 'a'. If they are moved, then they should not be deleted.
The first point was the
by value parameter. Calling append() invokes copy constructor. The default copy constructor that the compiler makes for you, since you don't define one. The default copies pointers, not the values that they point to.
1 2 3 4 5
|
U * x = ...;
// The default copy:
U * y = x;
// Explicit deep copy:
U * y = new U( *x );
|
The Rule of Three:
If a class
owns dynamic memory, then it should implement (1) copy constructor, (2) copy assignment, and (3) destructor. The compiler generated defaults will fail.