nakka soft world !

가상 소멸자 ( virtual destructor ) 본문

프로그래밍언어/C++

가상 소멸자 ( virtual destructor )

nakka 2017. 3. 27. 16:35
728x90

가상 소멸자 ( virtual destructor )

class Base

{

public:

void foo() { cout << "Base::foo" << endl; }


virtual ~Base() {} // 중요 라인

}


class Derived : public Base

{

public:

void foo() { cout << "Derived::foo" << endl; }


Derived() { cout << "자원 할당" << endl; }

~Derived() { cout << "자원 해지" << endl; }

}


int main()

{

Base* p = new Derived;  // Derived 생성자 호출


delete p; // 소멸자 호출


//p->foo();

}

728x90

'프로그래밍언어 > C++' 카테고리의 다른 글

추상클래스와 인터페이스  (0) 2017.03.27
순수 가상 함수와 추상 클래스  (0) 2017.03.27
객체 지향 디자인  (0) 2017.03.27
가상함수와 다형성  (0) 2017.03.27
상속의 개념  (0) 2017.03.25
Comments