Download presentation
Presentation is loading. Please wait.
Published byDorothy Harris Modified over 8 years ago
1
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating matrices dynamically Pointers and Dynamic Memory
2
2 Data Addresses in Memory int myInt; // &myInt == 5000 char myChar; // &myChar == 5004
3
3 Declare a pointer Pointer ptr is variable that can hold an address of an object of type Type Pointer Variables int* intPtr = NULL; Fish* fishPtr; Type* ptr;
4
4 Pointer Manipulations int m = 10; // Make intPtr point to (reference) m int* intPtr = &m; // *intPtr and m are aliases now string* sPtr; // No automatic init. sPtr = new string ("cool"); // Ctor call cout << *sPtr << " has " size () << " chars" ; // Delete what you “new”! delete sPtr;
5
5 Accessing Data with Pointers int x = 50, y = 100; int* px = &x; int* py = &y; y += *px; *py = 20; px = py;
6
6 Arrays and Pointers arr [0]arr[6]arr[5]arr[4]arr[3]arr[2]arr[1] arr+7arr+2arr+1 6000602460206016601260086004 6028 arr+5 Array name is const pointer to first element, i.e., arr == &arr[0]
7
7 Operator ‘new’ Use ptrs. and ‘new’ when 1)object lifetime must extend beyond block in which it’s declared; 2)you need to allocate memory at run time and can’t avoid it. Time24* p = new Time24 (); // *p is 00:00 (midnight) Time24* q = new Time24 (8, 15); // *q is 8:15 AM cout getHour (); cout << (*q).getMinutes (); p->advance (2, 15);... delete p; delete q;
8
8 Operator ‘delete’ Deallocating a dynamic array – use a slightly different form of delete (operator delete[ ]) – place square brackets [ ] between delete and the pointer variable name – system deallocates all of the memory originally assigned to the dynamic array T* arr = new T[SIZE]; // Allocated space for SIZE objects // arr[0]…arr[SIZE-1] delete[] arr; // Deallocate dynamic array storage
9
9 Classes with Pointer Members Recipe for classes that allocate memory dynamically – Destructor: ~C (); – Copy constructor: C (const C& cObject); – Operator=: C& operator= (const C& cObject); Default member-wise copy usually not appropriate
10
10 Copy Constructor / Overloaded Assignment Operator Time24 t1 (5, 30); // Invokes copy ctor Time24 t2 = t1; // or Time24 t2 (t1); // Default copy ctor will do // (For each field f) // init t2.f with t1.f // Invoke operator= t2 = t1; // Default will do // (For each field f) // t2.f = t1.f
11
11 A Dynamic Class (Uses ‘new’) class C { public: C (int x, int y) : m1 (x), m2 (new int (y)) { } ~C () { delete m2; } C (const C& co) // Must be const & : m1 (co.m1), m2 (new int (*(co.m2)) { } C& operator= (const C& co) { // new? if (this != &co) { m1 = co.m1; *m2 = *(co.m2); } return (*this); } private: int m1; int* m2; };
12
12 Copy Constructor C objB (objA);
13
13 “this” pointer Every non-static member function has a pointer to the invoking object this points to invoking object – If had to declare: (const) MyClass*const this; *this is the invoking object operator= returns *this, so can do – x = y = z;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.