4

Let me make this disclaimier : I have clear understanding of virtual function call in Constructor or Destructor.

In the below code I am trying to avoid virtual destructor ONLY FOR EXPERIMENTAL purpose.

Now my question is:

In main the call to Destroy fun calls the right virtual function. I am expecting any call to Destroy Function should call the right virtual fun.

But the same Destroy function placed in Base destructor call's the Base virtual function.

Is this related to static binding or compiler optimization?

class Base
{
public:
      Base()
      {
      }
      void Destroy()
      {
            callVirtual();
      }
      virtual void callVirtual()
      {
            cout<<"In Base callVirtual "<<endl;
      }
      ~ Base()
      {
           cout<<"In Base Destructor"<<endl;

           Destroy();
      }
};

.

class Derived : public Base
{
   public:
           Derived()
           {
           }
           void callVirtual()
           { 
               cout"<<In Derived callVirtual"<<endl;
           } 
};

.

int main()
{
    Base *pointer = new Derived();

    pointer->Destroy();    // Calls the right callVirtual

 return 0;

}
j0k
  • 22,600
  • 28
  • 79
  • 90
vrbilgi
  • 5,703
  • 12
  • 44
  • 60
  • 1
    I don't understand what your example code has to do with your question. Please could you clarify what the destructors have to do with anything? – Oliver Charlesworth Jan 30 '11 at 10:30
  • @Oil Charlesworh: In main the call to Destroy fun calls the right virtual function. I am expecting any call to Destroy Function should call the right virtual fun. Is this related to static binding in Destructor.????? But the same Destroy function placed in Base destructor call's the Base virtual function – vrbilgi Jan 30 '11 at 10:42
  • 1
    You have a clear understanding of calling virtual functions in constructor/destructor and you ask such questions. wtf? – BЈовић Jan 30 '11 at 11:47
  • @VJo: May be now I have clear understanding of virtual in cotr/Dstr – vrbilgi Jan 30 '11 at 16:39
  • "_I have clear understanding of virtual function call in Constructor or Destructor._" No you don't. – curiousguy Dec 26 '11 at 01:44

1 Answers1

6

In a destructor, the dynamic type of this is that of the current class, not the original dynamic type of the object. See e.g. http://www.artima.com/cppsource/nevercall.html.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • @Oil Charlesworth:Thanks for the clarification. Then their is no hack to achieve dynamic binding in constructor and destructor. – vrbilgi Jan 30 '11 at 10:51
  • @Matthieu: Invoking a member-function pointer on an object not of that class will lead to undefined behaviour. – Oliver Charlesworth Jan 30 '11 at 14:26
  • I was not thinking of a member-function pointer specifically actually. But you do raise an interesting point about the state of the object being destructed that my comment did not underlined :) – Matthieu M. Jan 30 '11 at 14:32
  • @vrbilgi "_Then their is no hack to achieve dynamic binding in constructor and destructor._" No hack is necessary. **This is dynamic binding.** – curiousguy Dec 26 '11 at 01:50
  • @OliCharlesworth "_Invoking a member-function pointer on an object not of that class will lead to undefined behaviour._" It will probably lead to a compiler error unless you use a cast (or another UB). – curiousguy Dec 26 '11 at 01:51