다른 클래스가 멤버로 Weapon 클래스를 가지고 있고 이를 NULL로 초기화 시키는 경우가 있어서
그럼 NULL 확인을 클래스 함수 안에서 해도 되나? 싶어서
아래와 같은 코드를 입력 해봤다.
std::string& Weapon::getType(void)
{
if (this)
return (this->type);
return (NULL);
}
'this' pointer cannot be null in well-defined C++ code;
pointer may be assumed to always convert to true.
라는 에러 메시지가 뜬다.
this는 늘 참인걸로 가정한다고 한다. NULL인 경우가 있다면 밖에서 어떻게 해줘야겠다.
'C++' 카테고리의 다른 글
[vector container] operator[] vs at (0) | 2022.09.14 |
---|---|
[C++] const vs constexpr (0) | 2022.09.01 |
[C++] 클래스 동적 배열 초기화 (0) | 2022.07.19 |
[C++] 인스턴스화 (instantiate) 란? (0) | 2022.07.16 |
[C++?] 인스턴스와 객체의 차이점? instance vs object (0) | 2022.07.16 |