Download presentation
Presentation is loading. Please wait.
Published byΊσις Ζωγράφος Modified over 6 years ago
1
Data Structures CSCI 132, Spring 2014 Lecture 12 Destructors, Copy Constructors and Overloading
2
Stacks as Linked lists top_node typedef Stack_entry Node_entry;
middle entry bottom entry typedef Stack_entry Node_entry; class Stack { public: Stack( ); bool empty( ); Error_code push( const Stack_entry &item); Error_code pop( ); Error_code top(Stack_entry &item) const; protected: Node *top_node; }
3
Clients creating garbage
for (int i = 0; i < ; i++) { Stack small; small.push(some_data); } Problem: small exists only within for loop. It is created repeatedly ( times!) and goes out of scope outside of loop. Memory is allocated, but not freed up. Solution: Use a Destructor function
4
The Stack destructor Stack :: ~Stack( ) { while (!empty( ) ) {
pop( ); //pop( ) deletes nodes } Destructors are called automatically when objects go out of scope. Destructor is indicated with the tilda.
5
Default Assignment Stack outer_stack;
for (int i =0; i < ; i++){ Stack inner_stack; inner_stack.push(some_data); inner_stack = outer_stack; } top entry middle bottom outer_stack.top_node inner_stack.top_node some_ data
6
Reference Semantics The problems discussed in this lecture occur because the linked list implementation of Stacks (and Queues) uses reference semantics. Reference semantics refers to the situation where we are working with addresses (pointers) rather than values. Value semantics refers to the situation when we are working with values. In the linked list version of the Stack class, the only data member is a pointer (reference semantics). This can cause problems when it is copied unless we provide functions to deal with them.
7
Overloading the assignment operator
We create our own Stack function to handle assignments between stacks: void Stack::operator = (const Stack &original); This function will be called when one Stack object is assigned to another. The following statements are equivalent: myStack.operator = (yourStack); myStack = yourStack;
8
Assignment with Stacks
To assign one stack to another one must: 1) Make a copy of the stack contained in the parameter (original). 2) Clear the stack being assigned to. 3) Move the copied data to the stack being assigned to.
9
Implementing the overloaded assignment operator
void Stack :: operator =(const Stack &original) //Overload assignment { }
10
Problems with copying stacks
void destroy_the_stack (Stack copy) { } int main() { Stack vital_data ; destroy_the_stack(vital_data); top entry middle bottom vital_data.top_node copy.top_node When the destructor for copy is called, the stack for vital_data is deleted!
11
Steps for Copying a Stack
The copy constructor must perform the following steps: Copy the NULL pointer if the stack to be copied is empty. Copy the first node. Use a loop to copy the other nodes.
12
The Copy Constructor Stack::Stack(const Stack &original) //copy constructor { }
13
The new Stack specification
class Stack { public: //Standard Stack methods Stack(); bool empty() const; Error_code push(const Stack_entry &item); Error_code pop(); Error_code top(Stack_entry &item) const; //Safety features for linked structures ~Stack(); Stack(const Stack &original); void operator =(const Stack &original); protected: Node *top_node; };
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.