Creating objects on stack or heap

来源:互联网 发布:剑灵可爱召唤捏脸数据 编辑:程序博客网 时间:2024/06/01 08:05
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.

0 0
原创粉丝点击