This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final : 부모 클래스의 특정 멤버 함수를 자식 클래스에서 재정의하지 못하도록 막을 때 사용 한다.
오버라이드는 간단히 부모 클래스에 선언 된 함수를 재정의 할 때나
이미 부모클래스에 있는 함수 다시 재정의 해본다아~~? 하고 절차를 확인할 때 사용 할 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
위 Base에 선언 된 가상함수 foo(int i) 를 override 한 것은 잘 되지만
아래 파라미터가 float 형인 것으로 override를 시도하면 컴파일 에러가 발생하게 됩니다.
개인적으로는 override를 이용하여 내가 부모클래스에 만들어 뒀던 가상함수를
다시 재정의하여 쓸 때 오타가 없는지 확인 하는 용도로도 썼었습니다.
간단한 예로 이렇게 활용했지요~!
Base Class 에 virtual void Update( float timeDelta); 라 만들어진 것을 // Update 가 대문자로 시작
Derived Class 에 virtual void update (float timeDelta) override; 라고 적으면 // update 가 소문자로 시작
이런식으로 적으면 바로 컴파일러가 update와 Update를 구분하여 너 철자 틀렸심. 하고 빨간줄을 그어줍니다.
두 번째 Final은 반대로 final이 선언 된 클래스를 끝으로 더이상 자식한테 재정의를 허용 하지 않을 때 사용합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
이번 글에서는 [6편] dafault / delete definition 에 대해서 살펴보도록 하겠습니다.
6. default / delete
default : 컴파일러가 함수를 자동으로 생성하도록 명시적으로 지정
delete : 컴파일러가 함수를 자동으로 생성하지 않도록 명시적으로 지정
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
위의 코드를 살펴보면 class TEST의 생성자와 소멸자는 default로 명시를 하는 것을 볼 수 있습니다.
default로 명시가 되었기 때문에 class TEST의 내용은 컴파일러가 함수를 자동으로 생성하게 됩니다.
반대로 class TEST2는 delete로 함수들이 명시적으로 지정되어 있습니다.
그리고 생성자와 void* operator new(size_t) = delete; 가 정의되어 있는 관계로
new 연산자를 이용한 할당이 블로킹 되어 있습니다.
두 번째 예로 다음 코드를 보도록 하겠습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
간단히 struct 안에 func(double d) 함수 선언하고 delete로 지정을 하였습니다.
그리고 main에서 func( double d ) 함수를 사용하려고 했지만!
Error가 빰빠카밤~ 하고 나오는걸 볼 수 있으실 겁니다.
아그럼 default는 자동으로 생성하는데 뭘해준다는거야?
라고 궁금해 하실까봐.
(사실 아 그냥 대충써... 하고 귀차니즘 발동하려다가 더 보충해봅니다..)
이번엔 default에 대한 예를 적어 보겠습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
위 코드를 보면 MyTest 클래스에서 기본적인 MyTest() 생성자가 기본으로 있습니다.
보통 기본 생성자는 우리가 따로 선언/정의를 해주지 않아도 컴파일러가 몰래 쓰담쓰담 만드는 게 상식입니다.
그렇다면 아래와 같이 코드를 바꾸면 어떻게 될까요?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters