250 likes | 408 Views
Pointers, Arrays, Destructors. Is this random stuff or are these somehow connected?. Pointers. What is a pointer? a memory address a special data type. Pointers. What do pointers look like? int * pNumber ; MyClass * pObject = NULL; Anything funny about pNumber above?
E N D
Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?
Pointers • What is a pointer? • a memory address • a special data type
Pointers • What do pointers look like? • int* pNumber; • MyClass* pObject = NULL; • Anything funny about pNumber above? • No – it is fine • Yes – it is uninitialized
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; What is the output? 99
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;
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;
Pointers -> Arrays • So, there is a connection from pointers to arrays!
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)
Arrays as class members class MyClass { public: // constructor // CRUD functions private: intmyValues[5]; unsigned int size; }; // Static array, array initialized when object is created // ints in myValuesnot initialized // size must be initialized in constructor
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
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?
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
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
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
Arrays as class members • How do you manually delete the dynamic members of a class? • the destructor
Arrays -> Destructors • So, there is a connection from arrays to destructors!
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
Destructors • What do they look like? class MyClass { public: ~MyClass (); };
Destructors • What do they look like? MyClass::~MyClass () { }
Destructors • Am I required to implement a destructor for every class that I create? • No – one is provided automatically
Destructors • When must I implement a destructor for my class? • when the class dynamically allocates memory (in most cases)
Destructor • Do I need a destructor for this class? class MyClass { public: // constructor // CRUD functions private: intmyValues[5]; unsigned int size; }; NO
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
Destructor • What would that destructor look like? MyClass::~MyClass () { delete [] myValues; }