본문 바로가기

C++

[C++] this 는 NULL인 경우가 있는가?

다른 클래스가 멤버로 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인 경우가 있다면 밖에서 어떻게 해줘야겠다.