c++ - linkList copy constructor and assignment operator -
i'm writing node , list classes , works fine except when include destructor , copy constructor , assignment operator functions in list class, , don't know wrong them or have miss not include.
linklist::linklist() :firstnode(null), lastnode(null), nodecount(0) {} linklist::~linklist()// destructor { node* current = firstnode; while( current != 0 ) { node* temp = current->getnextnode(); delete current; current = temp; } firstnode = 0; } linklist::linklist(linklist &l)// copy constructor { firstnode = null; nodecount = 0; node* temp = l.firstnode; for(int = 0; < l.getnodecount(); i++) { push_back(temp); temp = temp->getnextnode(); } } linklist& linklist::operator=(const linklist& l)// overloading assignemnt operator { linklist* ll; node* temp = l.firstnode; while( temp != null ) { ll->getlast(); temp = temp -> getnextnode(); } return *ll; }
your assignment should similar copy constructor. because both same thing.
the difference being assignment should clear
in list(itself) before starting copying rhs(the other one).
and should return reference itself. return *this
. assignments can chained.
linklist& linklist::operator=(const linklist& l)// overloading assignemnt operator { // check if self assignment if (&l == this) return *this; // clear myself. // copy other. return *this; }
Comments
Post a Comment