H-SC COMS 262 - Lecture 7 - Assignment Operator

Unformatted text preview:

The DestructorThe this PointerThe Assignment OperatorAssignmentTheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe Destructor and the AssignmentOperatorLecture 7Section 6.3Robb T. KoetherHampden-Sydney CollegeFri, Jan 30, 2009TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentOutline1The Destructor2The this Pointer3The Assignment Operator4AssignmentTheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe DestructorThe DestructorType::~Type(); // Prototype;The destructor destroys an object, i.e., it deallocatesthe memory used by the object.The destructor is not invoked explicitly.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentPurpose of the DestructorThe destructor is used to destroy an object when itpasses out of scope.A local variable passes out of scope when executionreturns from a function.A variable declared within a block {} passes out ofscope when execution leaves that block.A volatile object passes out of scope when theevaluation of the expression in which it occurs iscompleted.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentPurposes of the Default ConstructorThe Destructorint main(){Vectr v(5, 123);{Vectr u = 5*v;}return 0;}How many vectors get destroyed and exactly when?TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe Automatic DestructorThe automatic destructorInvokes each data member’s destructor.Deallocates the memory used by the data members.The automatic destructor does not deallocate memorythat the data members point to.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe this PointerEvery (non-static) member function has a hiddenparameter 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.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe this PointerWhen we write the prototype of a member function asApparent PrototypeType::func(params);the actual prototype isActual PrototypeType::func(Type*const this, params);this is a constant pointer to an object.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe this PointerFurthermore, when we write the prototype of a memberfunction asApparent PrototypeType::func(params) const;the actual prototype isActual PrototypeType::func(Type const*const this, params);this is a constant pointer to a constant object.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentUsage of the this PointerInside a member function, we refer to a data memberas member.This is interpreted as this->member.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentUsage of the this PointerInside a member function, we invoke another memberfunction of the same class by writing func(params).This is interpreted as this->func(params).TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe Assignment OperatorThe Assignment OperatorType& Type::operator=(const Type&); // ProtoObjectA = ObjectB; // UsageThe assignment operator assigns to an existing objectthe value of another existing object of the same type.The assignment operator must be a member function.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentPurpose of the Assignment OperatorThe assignment operator is use to assign the value ofone object to another object in an assignmentstatement.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentForm 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;}TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentForm of the Function operator=()The makeEmpty() and makeCopy() Functionsvoid makeEmpty();Clears out the old value of the object.void makeCopy(const Type& value);Assign the new value to the object.It is convenient write these two member functions andthen use them in the copy constructor, the destructor,and the assignment operator.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe Copy ConstructorThe Copy ConstructorType::Type(const Type& value){makeCopy(value);return;}TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe DestructorThe DestructorType::~Type(){makeEmpty();return;}TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe Assignment OperatorThe Assignment OperatorType& Type::operator=(const Type& value){if (this != &value){makeEmpty();makeCopy(value);}return*this;}TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentThe Automatic Assignment OperatorThe automatic assignment operator uses each datamember’s assignment operator to assign values tothem from the other object.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentMultiple AssignmentsThe assignment operator is right-associative.The statementa = b = c = d;is equivalent toa = (b = (c = d));TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentMultiple AssignmentsWhat about the statements((a = b) = c) = d;and(a = b) = (c = d);TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentExampleExample (The Vectr Class)Download vectr.h.Download vectr.cpp.Download and run VectrTest.cpp.TheDestructorand theAssignmentOperatorRobb T.KoetherTheDestructorThe thisPointerTheAssignmentOperatorAssignmentAssignmentHomeworkRead Section 6.3,


View Full Document

H-SC COMS 262 - Lecture 7 - Assignment Operator

Download Lecture 7 - Assignment Operator
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7 - Assignment Operator and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7 - Assignment Operator 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?