home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter07 / inventory_displayer_pointer_ver.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-11  |  927 b   |  35 lines

  1. // Inventory Displayer Pointer
  2. // Demonstrates passing constant pointers to constant objects
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. void display(const vector<string>* const pInventory);
  11.  
  12. int main()
  13. {
  14.     vector<string> inventory;
  15.     inventory.push_back("sword");
  16.     inventory.push_back("armor");
  17.     inventory.push_back("shield");  
  18.     
  19.     display(&inventory);
  20.  
  21.     return 0;
  22. }
  23.  
  24. //receive the address of inventory into the pointer pInventory
  25. //pInventory can be a constant pointer because the address it stores doesn't change
  26. //inventory can be accepted as a constant object because the function won't change it
  27. void display(const vector<string>* const pInventory)
  28. {
  29.     cout << "Your items:\n";
  30.     for (vector<string>::const_iterator iter = (*pInventory).begin(); 
  31.          iter != (*pInventory).end(); ++iter)
  32.          cout << *iter << endl;
  33. }
  34.  
  35.