저는 C++에서 초보자입니다. 나는 C++ oops 개념을 배우고 있습니다. 파생 클래스 (D)를 사용하여 기본 클래스 (B) 포인터의 메모리를 할당 할 수 있습니까?파생 클래스를 사용하여 기본 클래스 포인터의 메모리 할당
B *a = new D();
내 코드 :
또한#include <iostream>
using namespace std;
class B
{
public:
B()
{
cout<<"B constructor"<<endl;
}
~B()
{
cout<<"B Destroctur"<<endl;
}
};
class D : public B
{
public:
D()
{
cout<<"D constructor"<<endl;
}
~D()
{
cout<<"D Destroctur"<<endl;
}
};
int main()
{
B *a = new D();
delete a; // Is valid?
}
, 그것은 기본 클래스 포인터의 메모리를 무료로 유효합니까?
delete a;
http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors – SingerOfTheFall
"기본 클래스 포인터의 메모리"와 같은 것은 없습니다. – immibis
기본 사항을 배우기 시작하는 것이 좋습니다. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – user2079303