c++ - copy constructor for a stack class that uses list class -
c++ - copy constructor for a stack class that uses list class -
how phone call re-create constructor template class has private fellow member template object
i building stack class uses list class build stack. list class has re-create constructor, want re-create stack1 = stack2 how phone call in re-create constructor of stack re-create constructor
the lastly code re-create constructor stack class , trying re-create private fellow member list_3 mydata
when mydata = src.mydata; // copies same address , doesn't give new object
template <class itemtype> class list_3 { public: typedef size_t size_type; list_3(); list_3(const list_3 & src); ~list_3(); void insert(const itemtype & item); void remove(); void reset(); bool advance(); bool isempty() const; bool ateol() const; bool isfull() const; itemtype getcurrent() const; private: struct node { itemtype value; node* next; node* previous; }; node* head; node* tail; node* cursor; }; //copy constructor list class** template<class itemtype> list_3<itemtype>::list_3(const list_3<itemtype> & src) { //copy constructor head = null; tail = null; cursor = null; node *tempcursor = new node; tempcursor = src.head; // copying original list head tail if (!src.isempty()) { //if src list not empty start re-create process while (tempcursor != null) { insert(tempcursor->value); cursor = null; tempcursor = tempcursor->next; //move next item in list utilize previous if copying tail } reset(); //reset cursor } } **//============================================================================** template<class itemtype> class stack_3 { public: typedef int size_type; stack_3(); //copy constructor stack_3(const stack_3 & src); void makeempty(); bool isempty() const; bool isfull() const; void push(const itemtype &); itemtype pop(); private: list_3<itemtype> mydata; }; **//copy constructor stack class** template<class itemtype> stack_3358<itemtype>::stack_3358(const stack_3358<itemtype> & src) { mydata = src.mydata; }
you need initializer list accomplish this
stack_3358<itemtype>::stack_3358(const stack_3358<itemtype> & src) : mydata(src.mydata) // calls list_3 re-create constructor { } mydata = src.mydata; utilize re-create assigned operator utilize same address unless re-create assignment operator overloaded.
c++ class templates
Comments
Post a Comment