Creating objects on stack or heap

class Player { 
private:
int health; 
int strength; 
int agility;
public:
void move();
void attackEnemy(); 
void getTreasure();


};




int main(){
Player p1;
Player *p2 = new Player;
p1.move();
  p1.getTreasure();
  p2->attackEnemy(); 
  p2->move(); 
  p1.move();

}


When an object is allocated on the stack, we use the dot notation.

When an object is allocated on the heap, we use the arrow natation.

原文地址:https://www.cnblogs.com/cxchanpin/p/7364440.html