Hi All,
In below design i would like to know the appraoch of deletion of base class pointer .It is pritty clear that deletion of base class object obj won't take place inside
Draw destructor. Apart from this same code i have tested using unique_ptr and as expected it is working fine instead of using raw base class pointer.
#include<iostream>
using namespace std;
typedef enum result
{
result_error = 0,
result_success = 1
} RESULT_T;
class IShape
{
public:
RESULT_T draw ()
{
return Do_Draw();
}
virtual ~ IShape(){}
private:
RESULT_T virtual Do_Draw( )=0;
};
class Circle : public IShape
{
public:
RESULT_T Do_Draw()
{
cout<<"draw the circle ";
return result_success ;
}
};
class Rectangle : public IShape
{
int *p;
public:
Rectangle()
{
p=new int (10);
}
RESULT_T Do_Draw()
{
cout<<"draw the rectangle "<<endl;
return result_success ;
}
~Rectangle(){
cout<<"segmentation"<<endl;
delete p;
cout<<"deleting the destructor";
}
};
class Draw
{
IShape *obj;
public:
RESULT_T Draw_Shape(IShape* obj)
{
obj->draw();
}
~Draw( ){
//delete obj;segmentation fault scenario;
}
};
int main()
{
RESULT_T final_ret_code=result_error;
Draw obj;
final_ret_code=obj.Draw_Shape( new Circle);
final_ret_code=obj.Draw_Shape(new Rectangle);
return 1;
}