copy constructor/ assignement/ destructor
Bonjour,
Je travaille sur des listes chainees en c++.
J ai mon fichier MyLinkedList.h tel que:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #ifndef MYLINKEDLIST_H
#define MYLINKEDLIST_H
#include <iostream>
#include <string>
class MyLinkedList{
private:
struct node
{
std::string str;
node *next;
} *m_start;
public:
MyLinkedList(); //default constructor
MyLinkedList(const MyLinkedList& other); //copy constructor
MyLinkedList operator=(const MyLinkedList& other);
//void Add(const std::string& s );
//bool Remove(const std::string& s);
//bool InList(const std::string& s) const;
//void PrintList() const;
//int Count() const;
//void Sort();
~MyLinkedList(); // destructor
};
#endif |
L implementation des constructeurs, de l operateur = et du destructeur a lieu dans le fichier MyLinkedList.cpp et est telle que:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| MyLinkedList::MyLinkedList()
{
m_start=NULL;
}
MyLinkedList::MyLinkedList(const MyLinkedList& other)
{
m_start= other.m_start;
}
MyLinkedList MyLinkedList::operator=(const MyLinkedList& other)
{
m_start= other.m_start;
return *this;
}
MyLinkedList::~MyLinkedList()
{
if(m_start==NULL)
return;
while(m_start!=NULL)
{
node *temp;
temp = m_start->next;
delete m_start;
m_start=temp;
}
} |
Si j utilise le constructeur par defaut, tout va bien mais si j utilise le constructeur de copie, je recois une segmentation fault.
Avec valgrind j ai verifie que toute la memoire n est pas liberee a la fin, j ai donc un probleme dans mon destructeur que je n arrive pas a localiser.
Qqn a t il une idee?
Merci !!!