#include <iostream>
using namespace std;
class base
{
int a;
public:
base() {a =0;}
};
class derv :public base
{
int b;
public:
derv() {b =1;}
};
int main()
{
base *pb = new derv();
delete pb;
}
I don’t have a virtual destructor in derv class, does it delete only base part of derv object??
It might.
Because
basedoes not have a virtual destructor, your code exhibits undefined behavior. Anything might happen. It might appear to work as you expect. It might leak memory. It might cause your program to crash. It might format your hard drive.A citation was requested. C++11 §5.3.5/3 states that, for a scalar
deleteexpression (i.e., not adelete[]expression):The static type (
base) is different from the dynamic type (derv) and the static type does not have a virtual destructor, so the behavior is undefined.