The Destructor and the Assignment Operator - Lecture 6 Section 6

Preview:

Citation preview

The Destructor and the Assignment OperatorLecture 6

Section 6.3

Robb T. Koether

Hampden-Sydney College

Mon, Jan 25, 2010

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 1 / 30

Outline

1 The DestructorThe Automatic Destructor

2 The this Pointer

3 The Assignment OperatorThe Automatic Assignment Operator

3 Assignment

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 2 / 30

Outline

1 The DestructorThe Automatic Destructor

2 The this Pointer

3 The Assignment OperatorThe Automatic Assignment Operator

3 Assignment

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 3 / 30

The Destructor

The DestructorType::~Type(); // Prototype;

The destructor destroys an object, i.e., it deallocates the memoryused by the object.The destructor is not invoked explicitly.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 4 / 30

Purpose of the Destructor

The destructor is used to destroy an object when it passes out ofscope.A local variable passes out of scope when execution returns froma function.A variable declared within a block {} passes out of scope whenexecution leaves that block.A volatile object passes out of scope when the evaluation of theexpression in which it occurs is completed.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 5 / 30

Purposes of the Default Constructor

The Destructorint main(){

Vectr v(5, 123);{

Vectr u = 5*v;}return 0;

}

How many vectors get destroyed and exactly when?

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 6 / 30

Purposes of the Default Constructor

The Function operator*()Vectr operator*(double s, const Vectr& v){

return v.scalarMultiply(s);}

How many vectors get destroyed inside operator*()?

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 7 / 30

Purposes of the Default Constructor

The Function scalarMultiply()Vectr Vectr::scalarMultiply(double s) const{

Vectr v(mSize);for (int i = 0; i < mSize; i++)

v.element[i] = s * element[i];return v;

}

How many vectors get destroyed inside scalarMultiply()?

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 8 / 30

Outline

1 The DestructorThe Automatic Destructor

2 The this Pointer

3 The Assignment OperatorThe Automatic Assignment Operator

3 Assignment

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 9 / 30

The Automatic Destructor

The automatic destructorI Invokes each data member’s destructor.I Deallocates the memory used by the data members.

The automatic destructor does not deallocate memory that thedata members point to.In other words, if a data member is a pointer, then the automaticdestructor will probably create a memory leak.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 10 / 30

The Vectr Class

Example (The Vectr Class)The code.

I The header file vectr.h.I The implementation file vectr.cpp.I The test program Vectr Test.cpp.

The executable.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 11 / 30

Outline

1 The DestructorThe Automatic Destructor

2 The this Pointer

3 The Assignment OperatorThe Automatic Assignment Operator

3 Assignment

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 12 / 30

The this Pointer

Every (non-static) member function has a hidden parameternamed this.this is always the first parameter in such a function.this is a constant pointer to the object that invoked the memberfunction.

Type* const this

this provides us with a name for the invoking object.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 13 / 30

The this Pointer

When we write the prototype of a member function as

Apparent PrototypeType::func(params);

the actual prototype is

Actual PrototypeType::func(Type* const this, params);

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 14 / 30

The this Pointer

Furthermore, when we write the prototype of a member functionas

Apparent PrototypeType::func(params) const;

the actual prototype is

Actual PrototypeType::func(Type const* const this, params);

In this case, this is a constant pointer to a constant object.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 15 / 30

Usage of the this Pointer

Inside a member function, we refer to a data member by its name,e.g. mSize.It is interpreted as this->mSize.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 16 / 30

Usage of the this Pointer

Inside a member function, we invoke another member function ofthe same class by the function’s name, e.g.,scalarMultiply(5).It is interpreted as this->scalarMultiply(5).

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 17 / 30

Outline

1 The DestructorThe Automatic Destructor

2 The this Pointer

3 The Assignment OperatorThe Automatic Assignment Operator

3 Assignment

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 18 / 30

The Assignment Operator

The Assignment OperatorType& Type::operator=(const Type&); // PrototypeObjectA = ObjectB; // Usage

The assignment operator assigns to an existing object the value ofanother existing object of the same type.The assignment operator must be a member function.It can be invoked only through the operator =.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 19 / 30

Form of the Function operator=()

The Assignment OperatorType& Type::operator=(const Type& value){

if (this != &value){// Clear out the old value// Assign the new value}return *this;

}

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 20 / 30

Form of the Function operator=()

The makeEmpty() and makeCopy() Functionsvoid makeEmpty();

void makeCopy(const Type& value);

makeEmpty() clears out the old value of the object.makeCopy() assigns the new value to the object.It is convenient write these two member functions and then usethem in the copy constructor, the destructor, and the assignmentoperator (and the input() function).

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 21 / 30

The Copy Constructor

The Copy ConstructorType::Type(const Type& value){

makeCopy(value);return;

}

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 22 / 30

The Destructor

The DestructorType::~Type(){

makeEmpty();return;

}

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 23 / 30

The Assignment Operator

The Assignment OperatorType& Type::operator=(const Type& value){

if (this != &value){

makeEmpty();makeCopy(value);

}return *this;

}

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 24 / 30

The input() Function

The input() Functionvoid Type::input(istream& in){

makeEmpty(); // Avoid memory leak// Read the object}

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 25 / 30

Outline

1 The DestructorThe Automatic Destructor

2 The this Pointer

3 The Assignment OperatorThe Automatic Assignment Operator

3 Assignment

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 26 / 30

The Automatic Assignment Operator

The automatic assignment operator uses each data member’sassignment operator to assign values to them from the otherobject.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 27 / 30

Multiple Assignments

The assignment operator is right-associative.The statement

a = b = c = d;

is equivalent toa = (b = (c = d));

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 28 / 30

Multiple Assignments

What about the statements((a = b) = c) = d;

and(a = b) = (c = d);

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 29 / 30

Outline

1 The DestructorThe Automatic Destructor

2 The this Pointer

3 The Assignment OperatorThe Automatic Assignment Operator

3 Assignment

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 30 / 30

Assignment

HomeworkRead Section 6.3, pages 275 - 286.

Robb T. Koether (Hampden-Sydney College) The Destructor and the Assignment Operator Mon, Jan 25, 2010 31 / 30

Recommended