1. Home
  2. Computing & Technology
  3. C / C++ / C#

C++ Tutorial - Learn to use References

By David Bolton, About.com

7 of 7

Decoupled classes explained

To keep 8_4 simple, I've not shows how the map and army classes communicate with each other. For instance an army may wish to know what other armies are on the map and fight them. A display function may need to know what armies are located where and show them.

The gamedata class makes this possible. By declaring but not defining it before map and army classes are defined, both of these classes can access gamedata.

There would be other public member functions added to gamedata - e.g.

int getarmyidatlocation(int x,int y) ;
army & getarmyfromid(int id) ;
So for any location x,y, the function getarmyidatlocation() is called and if it returns a non zero id then getarmyfromid() returns a reference to the army. There's no guarantees that there is an army at every location so we can't just return a reference from the location coordinates. It needs these two function calls. Other functions we'd add would include addarmytomap() and removearmyfrommap().

This concludes this tutorial. However, we'll encounter references again in the tutorial on Copy Constructors which will appear shortly.

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. Decoupled classes explained

©2009 About.com, a part of The New York Times Company.

All rights reserved.