0

Suppose that I have a base class pointer, with base class having virtual functions. And if I am pointing a derived class object using base class pointer then how compiler knows that it is derived version of that virtual function to be called since at the time of assignment the derived class address would have been converted into base class type, why doesn't compiler generate an error since memory layout of both may be different.

And also if I have declared base class destructor virtual and deleting the base pointer having address of derived class then how does compiler know that I am actually deleting the derived object.

class a{
public:
virtual ~a(){}
virtual void f(){}
};
class b:public a
{
  public:
  ~b(){}
  void f(){ cout<<"hi";}
};
main()
{
  a *aptr;
  aptr=new b;   //here aptr has the address of b;
  delete aptr;  //since pointer type is base class so how will compiler know that
                //it has to call derived's destructor,also their names are different
                //as one is a and another is b. 
} 
Azazle
  • 37
  • 1
  • 9

2 Answers2

1

To actually delete a derived class through a base pointer you need to declare the destructor virtual. Otherwise havoc will ensue (actually undefined behavior, which is about the same).

As for how virtual functions are implemented: it is up to the implementation. Without asking about a specific implementation a real answer is not possible. The most likely implementation are vtables.

pmr
  • 58,701
  • 10
  • 113
  • 156
  • Yeah I forgot to write, just added the base dtor but issue is how compiler knows that it is call to derived's dtor – Azazle Dec 30 '12 at 19:10
  • @Azazle I think I answered that: You cannot know, unless you know which implementation (compiler) you are talking about. `vtables` are the most likely but nothing is guaranteed by the standard. – pmr Dec 30 '12 at 19:12
  • ok, but how using mere delete aptr compiler comes to know that it has to call derived dtor? – Azazle Dec 30 '12 at 19:16
  • @Azazle The compiler cannot statically know. That is why the runtime has to handle this situation and one such technique are vtables. – pmr Dec 30 '12 at 19:18
  • but vtables are used to identify calls for virtual functions rather than dtor. – Azazle Dec 30 '12 at 19:20
  • 1
    @Azazle There is no difference between a function and a destructor concerning this case. – pmr Dec 30 '12 at 19:23
  • A destructor is a member function. A virtual destructor is a virtual member function and so if the compiler uses a vtable then the virtual destructor will be in the vtable, just like any other virtual member function. – Jonathan Wakely Dec 30 '12 at 19:30
1

It won't (or it will). It's undefined behavior deleting an object of derived type through a pointer to a base type without a virtual destructor.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Yeah I forgot to write but issue is how compiler knows that it is call to derived's dtor – Azazle Dec 30 '12 at 19:09
  • @Azazle still not correct, it needs to be virtual. If it was, it knows (usually) through dynamic dispatch (just like virtual functions work). – Luchian Grigore Dec 30 '12 at 19:12