P
Peter
I *think* I understand the need for virtual functions. If you call a method
defined in Base, that method has no way of knowing about an overridden
functions in Derived.
Somehow, virtual functions allow methods in Base to "know" about other
functions that are redefined in Derived.
Now I'm trying to understand the need for virtual destructors. In this code:
#include <iostream>
using namepaste std;
class Base
{
public:
Base() { cout << "Constructor: Base" << endl; }
~Base(){ cout << "Destructor : Base" << endl; }
};
class Derived: public Base
{
public:
Derived() { cout << "Constructor: Derived" << endl; }
~Derived(){ cout << "Destructor : Derived" << endl; }
};
int main(void)
{
Base *Var = new Derived();
delete Var;
return 0;
}
the derived destructor is not called, and this is fixed by declaring the base
destructor virtual.
Unfortunately, this is an unsatisfying explanation: I don't understand why
the derived destructor isn't being called. I could certainly have a function
named foo() in both Base and Derived, and if I used:
Var->foo();
I would get Derived::foo(), not Base::foo().
So my first question is, why the difference? It seems more natural for
"delete Var" to call Derived::~Derived, not Base::~Base. After all, Var is
of type pointer to Derived, not pointer to Base. So _why_ is ~Derived() not
called in this case?
Secondly, there seems to be a disconnect between virtual functions and
virtual destructors.
Virtual functions help Base functions know about functions that are redefined
in Derived. In other words, to help old code know about new code.
Virtual destructors seem to help the Derived class know about its own
destructor.
I'm getting a flashback from when I was trying to sort out the many uses for
the word "static". Is there some common logical connection that explains why
we use the word "virtual" to do these seemingly two very different tasks?
Thanks!
Pete
defined in Base, that method has no way of knowing about an overridden
functions in Derived.
Somehow, virtual functions allow methods in Base to "know" about other
functions that are redefined in Derived.
Now I'm trying to understand the need for virtual destructors. In this code:
#include <iostream>
using namepaste std;
class Base
{
public:
Base() { cout << "Constructor: Base" << endl; }
~Base(){ cout << "Destructor : Base" << endl; }
};
class Derived: public Base
{
public:
Derived() { cout << "Constructor: Derived" << endl; }
~Derived(){ cout << "Destructor : Derived" << endl; }
};
int main(void)
{
Base *Var = new Derived();
delete Var;
return 0;
}
the derived destructor is not called, and this is fixed by declaring the base
destructor virtual.
Unfortunately, this is an unsatisfying explanation: I don't understand why
the derived destructor isn't being called. I could certainly have a function
named foo() in both Base and Derived, and if I used:
Var->foo();
I would get Derived::foo(), not Base::foo().
So my first question is, why the difference? It seems more natural for
"delete Var" to call Derived::~Derived, not Base::~Base. After all, Var is
of type pointer to Derived, not pointer to Base. So _why_ is ~Derived() not
called in this case?
Secondly, there seems to be a disconnect between virtual functions and
virtual destructors.
Virtual functions help Base functions know about functions that are redefined
in Derived. In other words, to help old code know about new code.
Virtual destructors seem to help the Derived class know about its own
destructor.
I'm getting a flashback from when I was trying to sort out the many uses for
the word "static". Is there some common logical connection that explains why
we use the word "virtual" to do these seemingly two very different tasks?
Thanks!
Pete