//CPP_Study.cpp
#include <iostream>
using namespace std;
#include "Player.h"
//직접 만든 헤더파일 include "..."
//c++에서 제공하는 라이브러리 include<...>
int main()
{
//Player는 몇 바이트?
//int 2개 = 8byte
//sizeof(Monster*) = 4 or 8
Player p1; //지역변수 (스택메모리)
Player* p2 = new Player(); //동적할당 (Heap)
return 0;
}
//Player.h
#pragma once
//#include "Monster.h" //include
//class Monster; //전방선언
class Player
{
//class는 설계도
public:
void KillMonster();
void KillMonster2() {
//_target->_hp = 0; //전방선언만으로는 몬스터 내의 멤버 함수나 변수를 사용할 수 없다.
}
public:
int _playerId;
int _hp;
int _attack;
//Monster _target; //#include "Monster.h"가 필수적
class Monster* _target; //전방선언으로 충분
//Player _target2; //정의중인 플레이어를 멤버클래스로 가질 수 없음
Player* _target2; //포인터는 주소값만 가지므로 플레이어의 포인터는 가질 수 있다
};
//Player.cpp
#include "Player.h"
#include "Monster.h"
void Player::KillMonster() {
//monster의 설계도가 없고 전방선언만으로 되어있을때는 아래 코드들은 돌아갈 수 없다.
//따라서 include "Monster.h"가 필요함
_target->_hp = 0;
_target->KillMe();
}
//Monster.h
#pragma once
class Monster
{
public:
void KillMe();
public:
int _monsterId;
int _hp;
int _defence;
//...
};
//Monster.cpp
#include "Monster.h"
void Monster::KillMe() {
_hp = 0;
}