Hi,
while executing below programme on my Dev-c++ 4.9.9.2 compiler , i was bit amazed as it should have not run properly due to use of deleted object (obj). can anybody let me know instead of crashing , it is getting executed without any memory issue.
~Mohan
#include<iostream.h>
#include<conio.h>
//virtual constructor
using namespace std;
class Base { public: virtual ~Base() {}
void virtual show(){cout<<"Base";}
virtual Base * create( ) const=0;
};
class Derived1 : public Base
{
public:
Derived1* create() const { return new Derived1; }
void show(){cout<<"Derived1";}
};
class Derived2 : public Base
{
public:
Derived2 * create() const { return new Derived2; }
void show(){cout<<"Derived2";}
};
int main()
{
Derived1 obj1;
Derived2 obj2;
Base * obj = obj1.create();
obj->show();
delete obj;
obj=obj2.create();
obj->show();
delete obj;
getch();
return 1;
}