E4-Unreal / test-simple-game-framework

simpleFramework for UE 5.1.0
MIT License
0 stars 1 forks source link

const에 관하여 #24

Open Eu4ng opened 1 year ago

Eu4ng commented 1 year ago

포인터 사용 시

변수

const char* test  // 포인터 test가 가리키는 값을 수정할 수 없음
char* const  test // 포인터 test 값을 수정할 수 없음
const char* const test // test 값과 test가 가리키는 값 모두 수정할 수 없음

함수

char Get() const // 멤버 변수를 수정할 수 없음

const 객체는 non-const를 함수를 호출하지 못한다.

참고 링크

Eu4ng commented 1 year ago

문제 상황

UEquipmentComponent::SelectEquipment()에서 상태 패턴 적용을 위해 UEquipmentState를 사용한다.

State 관리를 위해 UEquipmentComponent의 초기화 단계에서 MainState, SubState 객체를 생성하여 사용하는데 상태가 변경될 때마다 State 객체를 생성하는 것이 아니라 미리 생성해둔 각 상태 별 단 한 개의 객체 만을 사용한다.

그러므로 MainState와 SubState가 가리키는 상태 객체가 변해서는 안 되므로 const 키워드를 활용하고 싶은데, EquipmentComponent에서 EquipmentState의 non-const 함수를 호출하므로 const UMainState* MainState는 사용할 수 없다.

그렇다고 UMainState* const MainState는 선언 단계가 아닌, 초기화 단계에서 NewObject 할당하므로 사용할 수 없다.