This is something I can't understand. Let's say that I overload operator & to make and and between two objects, and operator = to make assignments (making a copy of the object). Let's suppose that I have a class named A, and this code:
A a1,a2,a3;
// initialize a1,a2,a3
a1=a2&a3;
If a1 was already allocated, when I assign a1 to a2&a3, there is a memory leak? is the new object created without deleting the old one ?
PS: Operators overloading using two arguments of type A (I haven't defined a class A, this is an example), returning an arguments of type A, defined as friend of the class.
The code:
It's a very long code, so I used pastebin: http://pastebin.com/42TnThfC But since it's long, I also post the most significative parts:
template<class T>
List<T>& List<T>::operator= (List<T>& l)
{
List<T>* ptr=l.next;
if(this!=&l)
{
resize(0);
while(!ptr->end)
{
push(ptr->info);
ptr=ptr->next;
}
}
return *this;
}
template <class T>
List<T>& operator& (List<T>& l1, List<T>& l2) throw()
{
List<T>* temp,*ptr=l1.next;
temp=new List<T>();
try
{
if( (l1.end)^(l2.end) )
throw excp1;
}
catch (char *s)
{
cout << "Exception: "<<s<<endl;
}
if(l1.end)
{
while(!ptr->end)
{
if(l2.in(ptr->info))
temp->push(ptr->info);
ptr=ptr->next;
}
}
else
{
if(l1.info==l2.info)
temp->push(l1.info);
}
return *temp;
}
It's a list, push pushes an item, an example of main:
int main(int arcg, char **argv)
{
List<T>l1,l2,l3;
for(int i=0;i<10;i++)
{
l1.push(i);
l2.push(i);
l3.push(i);
}
l3=l1&l2;
}
This case l3 is already l1&l2, but will this cause a memory leak?