#include <iostream>
using namespace std;
static int i=1;
class Parent
{
public:
virtual ~Parent(){10/i;}
};
class Child: public Parent
{
public:
virtual ~Child(){--i;}
};
int main()
{
Parent *ptr = new Parent;
Parent *ptr1 = new Child;
delete ptr;
delete ptr1;
//cout<<10/i;
return 0;
}
Why virtual destructor of the Parent class not providing any runtime error? Whereas the commented part of the code throws error when uncommented.
I guess your compiler optimizes the code and removes the useless part, including 10/i
in the base class destructor.
Try with this:
#include <iostream>
using namespace std;
static int i=1;
class Parent
{
public:
virtual ~Parent(){int tmp = 10/i; cout << tmp; }
};
class Child: public Parent
{
public:
virtual ~Child(){--i;}
};
int main()
{
Parent *ptr = new Parent;
Parent *ptr1 = new Child;
delete ptr;
delete ptr1;
//cout<<10/i;
return 0;
}