Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 0.88 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. static variable re-initializes
  2. static unsigned int masterID=0;
  3.  
  4. class game{
  5. public:
  6.    unsigned int m_id;
  7.    unsigned int m_players;
  8.  
  9.    // default constructor
  10.    game():m_id(masterID++){
  11.  
  12.    }
  13.  
  14.    // another constructor using a game instance
  15.    game(game g): m_id(masterID++){
  16.    ...
  17.    }
  18.  
  19.    // copy constructor
  20.    // copy constructor
  21.    game(const game &o)
  22.    : m_id(o.m_id), m_players(o.m_players)
  23.    {    }
  24.  
  25.    // assignment operator
  26.    game& operator =(const game o){
  27.     m_id = o.m_id;
  28.     m_players = o.m_players;
  29.     return *this;
  30. };
  31.        
  32. game g1, g2;
  33.        
  34. game g3(g2);
  35.        
  36. class game{
  37. public:
  38.     unsigned int m_id;
  39.     unsigned int m_players;
  40.     static unsigned int masterID; // no initialization!
  41.  
  42.    // default constructor
  43.    game():m_id(masterID++){
  44.  
  45.    }
  46.  
  47.    // another constructor
  48.    game(unsigned int players): m_id(masterID++), m_players(players){
  49.  
  50.    }
  51. };
  52.        
  53. game::masterID = 0;