1

I wrote a base and derived class that had no virtual functions. The use of virtual is typically the guide I see for when to use a virtual destructor.

However, while my classes have no virtual functions I am using the classes in a polymorphic way when I pass them around. Therefore, class Base should implement a virtual destructor?

class Base;
class Derived;

main()
{
   base& baseVar = derived();
   foo(baseVar);
}
user870130
  • 565
  • 1
  • 8
  • 16
  • Only use virtual destructors when you are going to be `delete`ing a pointer to a base class. That's the only time you need one. If you aren't going to be deleting in that way then make the base class's destructor protected to prevent errors. – Simple Mar 08 '14 at 13:15
  • "The use of virtual is typically the guide" - that's a useful rule of thumb, but the real guide is whether you need polymorphic destruction (such as deleting via a pointer to a base class). – Mike Seymour Mar 08 '14 at 13:29

2 Answers2

2

You should use a virtual destructor if your program will ever find itself in the position where it will be deleting an instance of a derived class through a base class pointer to ensure the correct destructor is called.

See this question for more detail When to use virtual destructors?

Community
  • 1
  • 1
mathematician1975
  • 21,161
  • 6
  • 59
  • 101
2

There is no polymorphism because you call (will call) non-virtual functions using the referemce. That is in your example you simply call (will call) functions of the base class.

Moreover this statement

base& baseVar = derived();

should not be compiled because you bind a temporary object with a non-const reference.

There must be

const base& baseVar = derived();

As for the destructor in your example then there is no need to have a virtual destructor. Becasue you defined a reference to a temporary object of the derived class. In this case for the temporary object will be called its own destructor. That is at first the destructor of the base class will be called (from the destructor of the derived class) and then the body of the destructor of the derived class will be executed.

The vertual destructor would be need if you would allocate a derived memory in the heap and assign the address of the allocated memory to a pointer of the base class. And when you would call operator delete for this pointer then if you have no virtual destructor then the only destructor of the base class would be called.

For example

class Base;
class Derived;

main()
{
   base* baseVar = new derived();
   delete baseVar; // base shall have a virtual destructor
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • As you showed my example doesn't need polymorphic destruction. However, I don't know how the user will use my classes. Your example shows that. That leads me to believe I should actively handle it by providing it or making it protected. I am not sure how I would make that decision, but I think either way would be fine – user870130 Mar 08 '14 at 14:40