In code:
struct Rep
{
const char* my_data_;
Rep* my_left_;
Rep* my_right_;
Rep(const char*);
};
typedef Rep& list;
ostream& operator<<(ostream& out, const list& a_list)
{
int count = 0;
list tmp = a_list;//----->HERE I'M CREATING A LOCAL COPY
for (;tmp.my_right_;tmp = *tmp.my_right_)
{
out << "Object no: " << ++count << " has name: " << tmp.my_data_;
//tmp = *tmp.my_right_;
}
return out;//------>HERE a_list is changed
}
I’ve thought that if I’ll create local copy to a_list object I’ll be operating on completely separate object. Why isn’t so?
Thanks.
I assume
listis meant to be the same asRep.You are only copying the the pointer to the first node in the list. You are not copying the data, nor the rest of the nodes of the list. You are doing a shallow copy of the first node of the list. If you would also copy the objects themselves it would be deep copy.