#include<iostream>
using std::cin;
using std::cout;
using std::endl;
class DynamicClass{
private:
public:
DynamicClass(){
cout<<"constructor call"<<endl;
}
~DynamicClass(){
cout<<"deconstructor call"<<endl;
}
};
int main(void){
DynamicClass normalClass; //일반 클래스 선언
DynamicClass* dClass= new DynamicClass(); //클래스 동적할당
delete(dClass); //클래스 free, deconstructor call
return 0; //return으로인해 일반클래스가(지역) 파괴될때 deconstructor call
}
포인터를 사용하지 않고 클래스를 선언할시에는 스택영역에 class가 올라간다.
그래서 main함수가 return(때에따라서 다른 함수내에서 쓴다면 그 함수가 종료될때)이 될때 deconstructor가 call된다.
만약 포인터를 사용한 클래스 선언으로 new를 이용하여 동적으로 클래스 메모리를 할당하였다면,
함수가 리턴되는것과는 다르게 delete로 memory free를 시켜줄때 deconstructor가 call된다.
'소프트웨어 > C/C++' 카테고리의 다른 글
| c/c++] virtual 소멸자를 이용한 메모리누수 방지 (0) | 2013.08.11 |
|---|---|
| c/c++] 객체포인터와 객체레퍼런스 (0) | 2013.08.11 |
| 객체] public, protected, private (0) | 2013.08.11 |
| c++] 동적할당실패시 예외처리 (0) | 2013.08.10 |
| c/c++] malloc은 void*를 리턴 (0) | 2013.08.10 |
| C/C++] 구조체의 크기 (0) | 2013.08.10 |
| 후위표기법 C언어 (0) | 2012.05.07 |