Download presentation
Presentation is loading. Please wait.
1
Pointer* Review Jason Stredwick
2
What is a pointer? It is the memory location of a variable
A pointer is made up of two pieces A number like an int The type of the variable pointed to
3
What does a pointer do? Allows access to the variable it points to
Allows a variable to be passed to a function without copying that variable Allows a variable to be changed within a function
4
Example One of the most important reasons to use a pointer is to save space by not copying data. For example: PolygonArray polyArray; // Millions of polygons void Draw1(PolygonArray p); void Draw2(PolygonArray *p);
5
How do you use a pointer? & returns the address of a variable int i;
&i; // Address of i * “dereference” a pointer, converts a pointer into the variable int *x = &i; *x = 3; // i = 3
6
How do you use a pointer cont.
-> used to access members of a class class A { public: int x; }; A *a = new A; a->x = 3; (*a).x = 4;
7
What is i1 and i2 after the function call Change?
Question 1 What is i1 and i2 after the function call Change? i1 == 3; i2 == 6; void Change(int a, int *b) { a = a + 1; *b = *b + 1; return; } int i1 = 3, i2 = 5; Change(i1, &i2);
8
What is *p1 after the call to Change?
Question 2 What is *p1 after the call to Change? *p1 == 3 void Change(int *a, int *b) { a = b; return; } int i1 = 3, i2 = 4; int *p1 = &i1, *p2 = &i2; Change(p1, p2);
9
Pointers and Const The four types of pointers given int i;
Data Pointer int * p1 = &i; // can do anything const int * p2 = &i; // can not change i int * const p3 = &i; // can not change p3 // but can change i const int * const p4 = &i; // can not change p4 // and can not // change i
10
Const Examples int i1 = 3, i2 = 5; int * const p1 = &i1;
p1 = &i2; // error, can not change p1 *p1 = 4; // ok cout << *p1 << endl; // Outputs 4 Const int *p2 = &i1; *p2 = 4; // error, can not change i1 p2 = &i2; // ok cout << *p2 << endl; // Outputs 5
11
this pointer In c++, every class has a this pointer associated with it. Within a class method is the only time you can use the this pointer. class Button { public: Button &operator=(const Button &b) { return *this; } void Push(void) { this->DisplayMessage(); } void DisplayMessage(void) { cout << “Button pressed” << endl; } };
12
Inheritance and pointers
Pointers allow you to use any part of an inheritance hierarchy, which can’t be done without pointers Allow for more complex data structures such as class interfaces.
13
Question 3 C original; C *cPointer = &original;
B *bPointer = &original; A *bPointer = &original; cPointer->Print(); bPointer->Print(); aPointer->Print(); What is the output? C class A { public: virtual void Print(void) { cout << “A” << endl; } }; class B : public A { { cout << “B” << endl; } class C : public B { { cout << “C” << endl; }
14
Question 4 C original; C *cPointer = &original; class A { public:
B *bPointer = &original; A *bPointer = &original; cPointer->Print(); bPointer->Print(); aPointer->Print(); What is the output? C B A class A { public: void Print(void) { cout << “A” << endl; } }; class B : public A { { cout << “B” << endl; } class C : public B { { cout << “C” << endl; }
15
Question 5 C original; B b = original; class A { b.Print(); public:
What is the output? B class A { public: virtual void Print(void) { cout << “A” << endl; } }; class B : public A { { cout << “B” << endl; } class C : public B { { cout << “C” << endl; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.