25
Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Embed Size (px)

Citation preview

Page 1: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Pointers, Arrays, DestructorsIs this random stuff or are these somehow connected?

Page 2: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Pointers

What is a pointer? a memory address a special data type

Page 3: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Pointers

What do pointers look like? int* pNumber; MyClass* pObject = NULL;

Anything funny about pNumber above? No – it is fine Yes – it is uninitialized

Page 4: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Pointers

What can I use pointers for? store the address of a static variable or object

access the variable or object through the pointer

int number = 10;int* pNumber = &number;*pNumber = 99;cout << number;

99What is the output?

Page 5: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Pointers

What can I use pointers for? dynamically allocate single variables or objects

string* pStr = new string(“hello”);

cout << (*pStr) << “ has “;cout << pStr->length() << “ characters.”;

delete pStr;pStr = NULL;

Page 6: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Pointers

What can I use pointers for? dynamically allocate arrays of variables or objects

MyClass* myObjects = new MyClass[10];myObject[0].setName(“Ziggy”);cout << myObject[0].getName();

delete [] myObjects;myObjects = NULL;

Page 7: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Pointers -> Arrays

So, there is a connection from pointers to arrays!

Page 8: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Pit stop: static vs. dynamic

What is the difference between a static variable or objects and a dynamic variable or object? static – allocation and cleanup are automatic

cleanup happens when the variable or object passes out of scope

dynamic – allocation and cleanup are manual the programmer (you) must do it all

new delete (remember “delete []” for arrays)

Page 9: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Arrays as class members

class MyClass{

public:// constructor// CRUD functions

private:int myValues[5];unsigned int size;

};

// Static array, array initialized when object is created// ints in myValues not initialized// size must be initialized in constructor

Page 10: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Arrays as class members

class MyClass{

public:

// constructor

// CRUD functions

private:int* myValues;unsigned int size;

};

// Dynamic array, must be initialized in constructor// size must be initialized in constructor

Page 11: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Arrays as class members

Remember class members can be static or dynamic

CRUD – something to think about Can you truly delete an element of an array of

variables or objects?

Page 12: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Arrays as class members

A challenging scenario What happens to the static members of a dynamic

object? when the programmer deletes the dynamic object,

the static members are cleaned up automatically

Page 13: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Arrays as class members

Another challenging scenario What happens to the dynamic members of a static

object? the programmer must manually delete the dynamic

members

Page 14: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Arrays as class members

And one last challenging scenario What happens to the dynamic members of a

dynamic object? the programmer must manually delete the dynamic

object and its dynamic members

Page 15: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Arrays as class members

How do you manually delete the dynamic members of a class? the destructor

Page 16: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Arrays -> Destructors

So, there is a connection from arrays to destructors!

Page 17: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Destructors

What are they? functions that perform cleanup on objects

When are they executed? when a static object passes out of scope when a dynamic object is deleted

Page 18: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Destructors

What do they look like?

class MyClass{

public:~MyClass ();

};

Page 19: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Destructors

What do they look like?

MyClass::~MyClass (){}

Page 20: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Destructors

Am I required to implement a destructor for every class that I create? No – one is provided automatically

Page 21: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Destructors

When must I implement a destructor for my class? when the class dynamically allocates memory (in

most cases)

Page 22: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Destructor

Do I need a destructor for this class?class MyClass{

public:// constructor// CRUD functions

private:int myValues[5];unsigned int size;

};

NO

Page 23: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Destructor

Do I need a destructor for this class?class MyClass{

public:// constructor// CRUD functions

private:int* myValues;unsigned int size;

};

MyClass::MyClass (){

myValues = new int[5];

size = 5;}

YES

Page 24: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Destructor

What would that destructor look like?

MyClass::~MyClass (){

delete [] myValues;}

Page 25: Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?

Finale