I've just had my lesson about virtual destructors and I have a question.
Lets say we have this code below:
#include <iostream>
class Base {
public:
virtual void fun() { std::cout << "Base Fun" << std::endl; };
virtual ~Base() { std::cout << "Base Destructor called" << std::endl; };
};
class Derived : public Base {
public:
virtual void fun() { std::cout << "Derived Fun" << std::endl; };
~Derived() { std::cout << "Derived Destructor called" << std::endl; };
};
int main()
{
Base* b = new Base();
Base* d = new Derived();
b->fun();
d->fun();
delete b;
delete d;
return 0;
}
We see that we have a Virtual destructor in our Base clase, which means when we delete d in our main, both the Base class destructor will be called and the Derived class destructor will be called..
**But what if we didnt have a destructor in our derived class, and we still wanted had the virtual destructor + we still wanted to delete d. What happens then? Does the derived class automatically "create" a destructor, and would the destructor then handle the ressource - d and delete it?