Calling a virtual function from a destructor in C++

#include <iostream>
using namespace std;
class base
{
public:
    ~base()
    {
        cout<<endl<<"ln virtual destructor";
    }
    virtual void fun1()
    {
        cout<<endl<<"ln base::fun1()";
    }
};
class derived:public base
{
public:
    ~derived()
    {
        base *p;
        p=this;
        p->fun1();
    }
    void fun1()
    {
        cout<<endl<<"ln derived::fun1()";
    }
};
int main()
{
  derived d;
    return 0;
}

Output

ln derived::fun1()
ln virtual destructor

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.