10.7 Pointers to Classes

10.7 Pointers to Classes

Just as pointers to any standard data type can be created, so can pointers to classes and data structures. In standard cases where pointers are not used, as we have already seen, objects such as Frog can be created and changed as follows:

      Frog frog;      frog.eat(); 

However, if dealing with a pointer to Frog, the notation changes somewhat. Instead, the pointer to member operator (->) is used to access class members and methods, as shown in the following code.

      Frog frog;      Frog *Pointer_To_Frog = &frog;      frog->eat(); 

10.7.1 this Pointer

Every C++ class has a special member pointer called this. (In other languages it is sometimes called me.) The this pointer of a class is simply a pointer to itself, and can be passed to any functions or any other classes to provide them with a pointer to this particular instance. Consider the following code:

      void Frog::Eat()      {         //do something here         this->Jump(); //Calls its own method      } 

Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net