1

This has been provided in my official notes but I spotted an error. Before I take it up to my instructor, I just thought of confirming it here with all my blood brothers- you guys.

#include <iostream.h>

class Base {
   public:
      virtual void display() { cout << "Base class" << endl; }
};

class Derived: public Base {
   // Nothing here
};

void main()
{
   Base * ptr;
   // Calls Base::display ( )
   ptr = new Base ;
   ptr ->display();
   delete ptr ;
   // Calls Base::display ( ) again
   ptr = new Derived ;
   ptr ->display();
   delete ptr ;
}

The output is:

Base class 
Base class

I think the problem is in the very last line of the main function. I mean, since the destructor is not virtual, I don't think you can delete a dynamically allocated instance of the derived class type using the pointer of the base class type. What do you think?

R Sahu
  • 204,454
  • 14
  • 159
  • 270
Blood Brother
  • 105
  • 1
  • 2
  • 7
  • Well, another problem is `#include `. There is no standard header with this name (there is however a standard header named ``). – Igor Tandetnik Apr 05 '15 at 04:10
  • possible duplicate of [Virtual destructor and undefined behavior](http://stackoverflow.com/questions/8599225/virtual-destructor-and-undefined-behavior) – vsoftco Apr 17 '15 at 01:38

2 Answers2

1

I mean, since the destructor is not virtual, I don't think you can delete a dynamically allocated instance of the derived class type using the pointer of the base class type. What do you think?

You can see here and here that this is undefined behavior.

If the base class has no virtual destructor the behavior of such code is simply undefined.

From the C++ standard:

5.3.5 Delete

3 In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined.

Community
  • 1
  • 1
James Adkison
  • 9,412
  • 2
  • 29
  • 43
  • It is, in fact, undefined behavior to do `delete ptrToBase;` where `ptrToBase` is of type `Base*` but actually points to an instance of `Derived*`, and `Base` lacks a virtual destructor. – Igor Tandetnik Apr 05 '15 at 04:06
  • @IgorTandetnik Yes, I was just looking that up myself – James Adkison Apr 05 '15 at 04:06
  • @IgorTandetnik I updated my answer with links to other SO answers. I was already in the process of catching my own mistake, but thanks for pointing it out. – James Adkison Apr 05 '15 at 04:11
0

When a function is defined as virtual then the compiler will call the function using a pointer to the base using dynamic binding, i.e. the call to the actual function will go through the class vTable to find the relevant implementation of the function. If a function is not defined as virtual then the code generated by the compiler will use static binding, i.e. the compiler will simply call the function it knows. In other words, if you call a non-virtual function on the base class pointer which points to a derived class then the compiler will generate code for calling the base class implementation.

Now, destructor behavior is just a private case of the general behavior described above. As in the absence of a destructor implementation the compiler will generate a default non-virtual destructor for you, the destruction of an object using a base class pointer will just call the base class destructor. You can reproduce this behavior it easily by explicitly defining the non-virtual destructors for both base and derived classes and use the debugger to see that the base class version is the one being called.

Amnon Shochot
  • 8,998
  • 4
  • 24
  • 30