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;
}

Another approach would be object instanciation through constructor of Draw object but
in this approach i need to create diff-2 objects (i.e. obj,obj2) per method.

class Draw
{
 IShape *obj;
public:


Draw (IShape *obj1):obj(obj1){ obj->draw();}

~Draw( ){
delete obj;
}


};

int main()
{
RESULT_T final_ret_code=result_error;
Draw obj( new Circle);
Draw obj1(new Rectangle);

return 1;
}

To avoid the segmentation fault below is the solution:
object destruction should have done in main method.

 int main()
    {
    RESULT_T final_ret_code=result_error;
    Draw obj;
    IShape *shape=new Circle;
    final_ret_code=obj.Draw_Shape(shape);
    delete shape;
    shape=new Rectangle;
    final_ret_code=obj.Draw_Shape(shape);
    delete shape;
    return 1;
    }

You need constructors for Draw that initialize the obj pointer appropriately (null for default ctor, and possibly copy ctor though in that case you may want to clone obj from the copied item - ditto an assignment operator). Then, deleting obj in the destructor will succeed, even if it is null - operator delete in C++ (these days) will ignore a null pointer. Note that you REALLY need an assignment operator as well since the one created by the compiler will happily copy everything from the right-hand-side of the expression, including the obj pointer, so you need to clone that as well, or not set it.

Remember, the compiler will by default create default and copy constructors, an assignment operator, and a destructor. It is not recommended to do this, but to implement them expressly to do what you need, or to declare them without implementing them so that use in code will result in a compiler error instead of a runtime error - much easier to fix!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.