Objects and nesting and pointers, oh my! If we have a pointer - - PowerPoint PPT Presentation

objects and nesting and pointers oh my if we have a
SMART_READER_LITE
LIVE PREVIEW

Objects and nesting and pointers, oh my! If we have a pointer - - PowerPoint PPT Presentation

Objects and nesting and pointers, oh my! If we have a pointer variable ptr , we access the thing that a pointer points to with the syntax *ptr To access a field or method of a class through an object variable, use the syntax


slide-1
SLIDE 1

Objects and nesting and pointers, oh my!

slide-2
SLIDE 2
  • If we have a pointer variable ptr, we access the thing

that a pointer points to with the syntax *ptr

  • To access a field or method of a class through an
  • bject variable, use the syntax

variable.field OR variable.method()

slide-3
SLIDE 3
  • So what if ptr points to an object?
  • string s = "Hello";

string *sptr = &s; string *sptr2 = new string("Goodbye");

  • We access the string through the pointer using the

same syntax: cout << s; // regular access cout << *sptr; // through pointer cout << *sptr2; // through pointer

slide-4
SLIDE 4
  • Problem occurs if we want to get the length of the

string through the pointer:

  • string s = "Hello";

string *sptr = &s; cout << *sptr.length(); // error

  • Reason: the dot operator has higher precedence than

the dereference (*) operator, so C++ interprets this as: cout << *(sptr.length());

slide-5
SLIDE 5
  • Two ways to fix this.
  • Method 1: Use parentheses to change order of
  • perations:
  • string s = "Hello";

string *sptr = &s; cout << (*sptr).length(); // OK

  • Method 2: Use the arrow operator, which combines

the dereference * and dot operator into one:

  • cout << sptr->length();

// OK

  • Method 2 is much more common.
slide-6
SLIDE 6

Rule

  • To access a field or method of a class through an
  • bject variable, use the syntax

variable.field OR variable.method()

  • To access a field or method of a class through a

pointer to an object, use the syntax ptr->field OR ptr->method();

slide-7
SLIDE 7

class thingy { public: int x; void f(); };

slide-8
SLIDE 8

class thingy { public: int x; void f(); }; thingy thing; cout << thing.x; thing.f(); thingy *thing_ptr = &thing; cout << thing_ptr->x; thing_ptr->f(); thingy *tptr2 = new thing; cout << tptr2->x; tptr2->f();

slide-9
SLIDE 9

class thingy { public: int x; void f(); }; vector<thingy> tvec; // add things to tvec cout << tvec[0].x; tvec[0].f(); vector<thingy*> ptrvec; // add things to ptrvec cout << ptrvec[0]->x; ptrvec[0]->f();

slide-10
SLIDE 10

class thingy { public: int x; void f(); }; class doohickey { public: int y; void g(); thingy t; thingy *tptr; }

slide-11
SLIDE 11

class thingy { public: int x; void f(); }; class doohickey { public: int y; void g(); thingy t; thingy *tptr; }

doohickey doohick; cout << doohick.y; doohick.g(); cout << doohick.t.x; doohick.t.f(); doohickey *dptr = &doohick; cout << dptr->y; dptr->g(); cout << dptr->t.x; dptr->t.f();

slide-12
SLIDE 12

class thingy { public: int x; void f(); }; class doohickey { public: int y; void g(); thingy t; thingy *tptr; }

doohickey doohick; doohickey *dptr = &doohick; doohick.tptr = new thingy; cout << doohick.tptr->x; doohick.tptr->f(); cout << dptr->tptr->x; dptr->tptr->f();

slide-13
SLIDE 13

class thingy { public: int x; void f(); }; class doohickey { public: int y; void g(); thingy t; thingy *tptr; }

vector<doohickey> dvec; vector<doohickey*> dptrvec; // add stuff to vectors cout << dvec[0].t.x; cout << dvec[0].tptr->x; cout << dvecptr[0]->t.x; cout << dptrvec[0]->tptr->x;