overload operator=

Hi!

I want to overload the operator =, but my object has pointers, so when I do the return, eventually it passes a copy and so my destructor kills it... how to prevent the desctructor killing? my code is the following

thx in advance!

Section Section::operator=(const Section &section)
{
(*this).sigle_ = "aaaa";// section.sigle_;
(*this).local_ = section.local_;
(*this).titre_ = section.titre_;
(*this).nombreEtudiants_ = section.nombreEtudiants_;

//Create a copy of the pointers
if (section.prof_ != 0)
{
(*this).prof_ = new Professeur;
*((*this).prof_) = *(section.prof_);
}


for (int i = 0; i < MAX_ETUDIANT; i++)
{
*((*this).etudiant_[i]) = *(section.etudiant_[i]);
}

return (*this);
}
Please use the code tags: http://www.cplusplus.com/articles/jEywvCM9/

If your class "owns" dynamically allocated memory, then you should implement not only the destructor and copy assignment, but also the copy constructor.

Please show at least the class definition and destructor.
http://www.cplusplus.com/doc/tutorial/classes2/
Scroll down to "Copy assignment" and it has exactly the same case, object with pointers.

But there are always alternatives, like not using pointers, or using smart pointers.
Topic archived. No new replies allowed.