Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Destructor and the Assignment Operator

Similar presentations


Presentation on theme: "The Destructor and the Assignment Operator"— Presentation transcript:

1 The Destructor and the Assignment Operator
Lecture 8 Thu, Feb 8, 2007

2 Four Fundamental Member Functions
The Destructor The destructor destroys an object, i.e., it deallocates the memory used by the object. Prototype: Usage: The destructor is not invoked directly. Type::~Type(); 2/22/2019 Four Fundamental Member Functions

3 Example: The Vector Class
vectr.h vectr.cpp VectrTest.cpp 2/22/2019 Four Fundamental Member Functions

4 Purpose of the Destructor
The destructor is used to destroy an object when it passes out of scope. A local variable passes out of scope when execution returns from a function. A variable declared within a block {} passes out of scope when execution leaves that block. A volatile object passes out of scope when the evaluation of the expression in which it occurs is completed. 2/22/2019 Four Fundamental Member Functions

5 The Automatic Destructor
Invokes each data member’s destructor. Deallocates the memory used by the data members. The automatic destructor does not deallocate memory that the data members point to. 2/22/2019 Four Fundamental Member Functions

6 Four Fundamental Member Functions
The this Pointer Every (non-static) member function has a hidden parameter named this. this is always the first parameter in such a function. this is a constant pointer to the object (Type* const this) that invoked the member function. this provides us with a name for the invoking object. 2/22/2019 Four Fundamental Member Functions

7 Four Fundamental Member Functions
The this Pointer When we write the prototype of a member function as the actual prototype is this is a constant pointer to an object. Type::Function(Parmeters); Type::Function(Type* const this, Parmeters); 2/22/2019 Four Fundamental Member Functions

8 Four Fundamental Member Functions
The this Pointer Furthermore, when we write the prototype of a member function as the actual prototype is this is a constant pointer to a constant object. Type::Function(…) const; Type::Function(Type const* const this, …); 2/22/2019 Four Fundamental Member Functions

9 Usage of the this Pointer
Inside a member function, we refer to a data member as This is interpreted as member-name this->member-name 2/22/2019 Four Fundamental Member Functions

10 Usage of the this Pointer
Inside a member function, we invoke another member function by writing This is interpreted as function(parameters); this->function(parameters); 2/22/2019 Four Fundamental Member Functions

11 The Assignment Operator
The assignment operator assigns to an existing object the value of another existing object of the same type. Prototype: Usage: The assignment operator must be a member function. Type& Type::operator=(const Type&); ObjectA = ObjectB; 2/22/2019 Four Fundamental Member Functions

12 Purpose of the Assignment Operator
The assignment operator is use to assign the value of one object to another object in an assignment statement. 2/22/2019 Four Fundamental Member Functions

13 Form of the Function operator=()
Type& Type::operator=(const Type& value) { if (this != &value) // Clear out the old value // Assign the new value } return *this; 2/22/2019 Four Fundamental Member Functions

14 Form of the Function operator=()
Typically we write two member functions makeEmpty() makeCopy() Type& Type::operator=(const Type& value) { if (this != &value) makeEmpty(); // Clear out old value makeCopy(value); // Copy new value } return *this; 2/22/2019 Four Fundamental Member Functions

15 The Copy Constructor and the Destructor
Then we can also write the copy constructor and destructor more easily. Type::Type(const Type& x) { makeCopy(x); return; } Type::~Type() makeEmpty(); 2/22/2019 Four Fundamental Member Functions

16 The Automatic Assignment Operator
Uses each data member’s assignment operator to assign values to them from the other object. 2/22/2019 Four Fundamental Member Functions

17 Four Fundamental Member Functions
Multiple Assignments The assignment operator is right-associative. The statement is equivalent to a = b = c; a = (b = c); 2/22/2019 Four Fundamental Member Functions

18 Four Fundamental Member Functions
Multiple Assignments What about the statement (a = b) = c; 2/22/2019 Four Fundamental Member Functions


Download ppt "The Destructor and the Assignment Operator"

Similar presentations


Ads by Google