Download presentation
Presentation is loading. Please wait.
Published byClifford Parsons Modified over 8 years ago
1
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to excellent effect) in classes. – Linked List class – Dynamic storage class – And More ● However, you must be careful when using dynamic memory in classes.
2
The this pointer ● The this pointer is a predefined pointer in every non-static member function of a class. ● It points to the calling object. ● So, if object x calls a function which uses the this pointer, this-> will access the members of x. ● This cannot be used in static function, and it cannot be changed. It will always point to the calling object.
3
The this pointer ● Normally, the use of this makes no difference. void Sample::showStuff(){ cout << stuff; } void Sample::showStuff(){ cout << this.stuff; } ● In this scenario, using this makes no difference. ● So, when does using this come in handy?
4
Overloading the Assignment Operator ● As we have discussed, the assignment operator copies the right-hand value into the left hand value, and returns a reference to the left-hand value. ● This allows you to do things like a = b = c; (a = b).f(); ● The reference is returned to allow you to use the newly assigned value as you need to.
5
Overloading the Assignment Operator ● Normally, a default assignment operator is created, that just copies the values from the right hand side's members into the left hand side's members. ● This is sufficient if your class has no dynamic members... ● But what if it does have dynamic members?
6
What could go wrong ● There are a variety of ways things could go wrong, but most have the same flavor: – Assume you have two objects which store dynamic lists, list1 and list2. – You run list1 = list2; – Then you try to modify the list stored in list1. – You are changing list2! ● Another problem is if your dynamic storage needs to be overwritten – what if you don't delete the old dynamic array? Memory Leak!
7
Overloading the Assignment Operator ● So, here is an example. There's a potential error here; can you spot it? StringClass& StringClass::operator =(const StringClass rightSide){ capacity = rightSide.capacity; length = rightSide.length; delete [] a; a = new char[capacity]; for(int i = 0; i < length; i++) a[i] = rtSide.a[i]' return *this; }
8
The potential error ● What if you (or some unsuspecting end user) tried executing the command s = s; ? ● You'll end up destroying the whole array, and then having nothing to copy. This would be extremely damaging. ● But, you can use this to error-check
9
Overloading the Assignment Operator - Fixed ● Here is the fixed example. StringClass& StringClass::operator =(const StringClass rightSide){ // If they are the same, just return the // object if(this = &rightSide){ return *this; } capacity = rightSide.capacity; length = rightSide.length; delete [] a; a = new char[capacity]; for(int i = 0; i < length; i++) a[i] = rtSide.a[i]' return *this; }
10
Overloading the assignment operator ● So, it is important to overload the assignment operator whenever you have dynamic values in the class. – Make sure you don't have two separate objects end up with two of the same references. – Make sure you delete any dynamic variables that need to be deleted. ● There is an important notion here – the assignment operator is one of the important things you need to know, and it is related to the concept of deep copy versus shallow copy.
11
Deep Copy or Shallow Copy? ● Shallow copy is when you just copy the values in an object's member variables to another object's member variables. – OK if you only have doubles and ints or other normal types. – Not OK if you have pointers! ● Deep copy is when you create copies of what each pointer in the target is pointing to. – Copy a dynamic variable/array by allocating new space and transferring the values stored over. – What happens if one of the class' member variables is a pointer to another class? What if THAT class has dynamic variables?
12
Copy Constructor ● We need a way to quickly copy the values of a class – we can do this with a copy constructor. ● This is a constructor defined like any other that takes a single const call-by-reference parameter of the same type as the class. ● It should perform a deep copy, and return a new object which has the same values but it independent as the parameter object.
13
Copy Constructors ● Something to know (which will be brought up again shortly) – the copy constructor is what is called whenever you pass an object as call-by-value. ● A default copy constructor exists, but it only performs a shallow copy. ● You should (for reasons we will soon discuss) always make a copy constructor if your class has pointers or uses new.
14
Destructors ● A destructor is a member function of a class which is responsible for eliminating dynamic variables in an object. ● The destructor is called whenever an object passes out of scope (like a local variable in a function, say). ● The main idea is to eliminate dynamic memory that needs to be freed. ● Syntax: ~ (); // Prototype ~ (){...} // Code
15
The Big Three ● These three things are commonly referred to as the “big three.” This is because if you use one of them, you should use all three of them. ● If you only make one or two of these, they can interact in ways that may be unpredictable.
16
Big Three Interaction ● Examples are in the textbook in Ch. 10.3. ● Some of the examples: – If you have a destructor but no copy constructor, and you have a function that takes a pass-by-value reference to your class type, you will accidentally destroy the object passed in. – If you have a copy constructor but no destructor, you may accidentally have memory leaks.
17
Project 1 ● Due July 10th, 11:59 pm. ● Now available online. ● Start it TODAY IN LAB. ● I will be in lab with you today since Scott is not available.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.