Let's say that I have a class A:
class A
{
private:
int value;
public:
A(int value)
{
this->value=value;
}
A& operator= (A& obj)
{
value=obj.value;
}
};
There is a problem if I use the contextual instantiation:
A obj;
obj=*new A(5);
This case the new object created is not deleted and I lose it's reference. So I could put in the operator= overloading function the instruction to delete the object which is right value of the assignment, but if I do this:
A obj1,obj2;
obj1=obj2;
So how to avoid to waste memory? There's a way?
@Oli Charlesworth : But then how are iterators implemented? For example in std::list if I say:
list<string> l;
// initialize the list elements
list<string>::iterator i=l.begin();
This is an assignment, is another object of kind list::iterator created?