C++ 에는 생성자와 소멸자라는 개념이 존재한다.
간단히 말해서 클래스가 시작될 때와 클래스가 소멸될 때 실행되는 이벤트다.
#include <iostream>
using namespace std;
class Player {
public://member func
Player(string newName, int newHp, int newMp) : name(newName), hp(newHp), mp(newMp) { //생성자
cout << "hi" << endl;
}
~Player() { //소멸자
cout << "byebe";
}
public://member var
string name;
int hp;
int mp;
};
int main() {
Player me("UniCoti", 50, 60);
}
가령 이런 코드가 있다고 가정하면, Player() { }는 생성자, ~Player() { }는 소멸자이다.
중괄호 안에 코드를 작성하면 생성/소멸 시에 원하는 코드를 실행시킬 수 있다.
객체지향 언어인 만큼 객체 생성과 동시에 값들을 넘겨주고 싶을 수 있는데,
이는 생성자에서 처리하면 좋다. 직접 대입해도 되지만 위처럼 : var(인수)를 사용하면
생성자 정의만으로 위 기능이 가능해진다.
이상으로 도움이 되었길 바라며,
끝.
'게임 개발 > Unreal Engine 5' 카테고리의 다른 글
| Unreal Engine C++] 객체지향 4원칙 (0) | 2025.11.05 |
|---|---|
| Unreal Engine 5] BluePrint에서의 상속 (0) | 2025.10.24 |
| Unreal Engine 5] Is valid? 옵션 (0) | 2025.10.13 |
| Unreal Engine 5) Blueprint 실행 경로 추적하기, CallStack (0) | 2025.07.29 |
| Unreal Engine 5, Blueprint) Gate, MultiGate, Do Once, Do N (6) | 2025.06.09 |
댓글