Who calls the Destructor of the class when operator delete is used in multiple inheritance.
        Posted  
        
            by dicaprio-leonard
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by dicaprio-leonard
        
        
        
        Published on 2010-04-07T11:50:39Z
        Indexed on 
            2010/04/07
            11:53 UTC
        
        
        Read the original article
        Hit count: 350
        
c++
|inheritance
This question may sound too silly, however , I don't find concrete answer any where else.
With little knowledge on how late binding works and virtual keyword used in inheritance.
As in the code sample, when in case of inheritance where a base class pointer pointing to a derived class object created on heap and delete operator is used to deallocate the memory , the destructor of the of the derived and base will be called in order only when the base destructor is declared virtual function.
Now my question is :
1) When the destructor of base is not virtual, why the problem of not calling derived dtor occur only when in case of using "delete" operator , why not in the case given below:
derived drvd;
base *bPtr;
bPtr = &drvd; //DTOR called in proper order when goes out of scope.
2) When "delete" operator is used, who is reponsible to call the destructor of the class? The operator delete will have an implementation to call the DTOR ? or complier writes some extra stuff ? If the operator has the implementation then how does it looks like , [I need sample code how this would have been implemented]. 
3) If virtual keyword is used in this example, how does operator delete now know which DTOR to call?
Fundamentaly i want to know who calls the dtor of the class when delete is used.
 Sample Code 
class base
{
    public:
    base()
    {
       cout<<"Base CTOR called"<<endl;
    }
virtual ~base()
{  
   cout<<"Base DTOR called"<<endl;
}
};
class derived:public base
{
    public:
        derived()
        {
            cout<<"Derived CTOR called"<<endl;
        }
~derived()
 {
        cout<<"Derived DTOR called"<<endl;
 }
};
I'm not sure if this is a duplicate, I couldn't find in search.
int main()
{
    base *bPtr = new derived();
delete bPtr;// only when you explicitly try to delete an object
return 0;
}
© Stack Overflow or respective owner