Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: " Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?"— Presentation transcript:

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

2 Pointers  What is a pointer?  a memory address  a special data type

3 Pointers WWhat do pointers look like? iint* pNumber; MMyClass* pObject = NULL; AAnything funny about pNumber above? NNo – it is fine YYes – it is uninitialized

4  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?

5 Pointers  What can I use pointers for?  dynamically allocate single variables or objects string* pStr = new string(“hello”); cout << (*pStr) << “ has “; cout length() << “ characters.”; delete pStr; pStr = NULL;

6 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;

7 Pointers -> Arrays  So, there is a connection from pointers to arrays!

8 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)

9 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

10 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

11 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?

12 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

13 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

14 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

15 Arrays as class members  How do you manually delete the dynamic members of a class?  the destructor

16 Arrays -> Destructors  So, there is a connection from arrays to destructors!

17 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

18 Destructors  What do they look like? class MyClass { public: ~MyClass (); };

19 Destructors  What do they look like? MyClass::~MyClass () { }

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

21 Destructors  When must I implement a destructor for my class?  when the class dynamically allocates memory (in most cases)

22 Destructor  Do I need a destructor for this class? class MyClass { public: // constructor // CRUD functions private: int myValues[5]; unsigned int size; }; NO

23 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

24 Destructor  What would that destructor look like? MyClass::~MyClass () { delete [] myValues; }

25 Finale


Download ppt " Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?"

Similar presentations


Ads by Google