Factory method in C++ Interview question description: What approach you will follow to delete pointers returned by Factory method design pattern in C++ in main program? For example, below is given factory method class.
//Factory method design class
class ShapesFactory{
public:
static IShape* getInstance(SHAPETYPE Type){
if( Type == CIRCLE){
return new Circle;
}else if( Type == SQUARE){
return new Square;
}else if( Type == RECTANGLE){
return new Rectangle;
}
}
};
Answer: Smart pointer in C++ can be used to delete pointers of object returned by Factory method in C++ in the client code for example in main() method. Smart pointer will handle memory deallocation itself instead of manual call.
Recommended to read this post before moving forward: How to write Smart Pointer for a given class in C++?
NOTE: With this interview question, interviewer wants to know if we’re familiar with Smart pointer and what approach we follow to handle memory deallocation for object pointers returned by factory method in C++. Also, they want to know if you can write smart pointer class and familiar about existing auto_ptr or shared_ptr smart pointers in C++ etc. Below, we will see implementation of Smart pointer in C++ and how to use auto_ptr also.
First, let’s have an example of factory method that returns shape object pointers for multiple type of shapes e.g. Circle, Square and Rectangle etc.
getInstance(SHAPETYPE Type)method of ShapesFactory class will return shapes object pointers in below program.
#include <iostream>
using namespace std;
//
// Smart Pointers
enum SHAPETYPE{
CIRCLE,
SQUARE,
RECTANGLE
};
//Shape classes
class IShape{
public:
void virtual draw()=0;
};
class Circle:public IShape{
void draw(){
cout<<"Circle"<<"\n";
}
};
class Square:public IShape{
void draw(){
cout<<"Square"<<"\n";
}
};
class Rectangle:public IShape{
void draw(){
cout<<"Rectangle"<<"\n";
}
};
Shape factory that returns multiple type of shapes
class ShapesFactory{
public:
//Factory method that returns instance of multiple type
//of shapes
static IShape* getInstance(SHAPETYPE Type){
if( Type == CIRCLE){
return new Circle;
}else if( Type == SQUARE){
return new Square;
}else if( Type == RECTANGLE){
return new Rectangle;
}
}
};
If we don’t use smart pointer we have to explicitly call delete on pointers to deallocate memory. For example,
int main(){
//Get shapes from factory without using smart pointer
IShape * circlePtr = ShapesFactory::getInstance(CIRCLE);
IShape * Squarptr = ShapesFactory::getInstance(CIRCLE);
//Explicitly call delete to deallocate memory.
delete circlePtr;
delete circlePtr;
return 0;
}
But, if we use smart pointer, delete operation will be handled automatically. Below, we will create a smart pointer class and use it and also, we will use already available smart pointer i.e. auto_ptr.
Example explained with proper comments.
//Shape Smart Pointer
class SPShape{
IShape *_pointer;
public:
SPShape(IShape *pointer):_pointer(pointer){
}
// call delete on shape pointer
~SPShape(){
cout<<"Deleting object"<<"\n"; delete _pointer; } //overload "*" in case we need to deference object. IShape& operator* () { return *_pointer; } //Overload -> operator that will be used to
//call functions of classes
IShape* operator-> ()
{
return _pointer;
}
};
void TestFunction(){
//Get Circle object
SPShape cptr = ShapesFactory::getInstance(CIRCLE);
cptr->draw();
// Get square object
SPShape sptr = ShapesFactory::getInstance(SQUARE);
sptr->draw();
//When Test function ends these objects will go out of scope
//and destructor of SPShape will be automatically called and object
//will get deleted.
}
//Use Auto_pointer : This is the smart pointer provide std name space.
// need to include #include header file.
//An auto_ptr is simply an object that holds a pointer for us within a function.
//Holding a pointer to guarantee deletion at the end of a scope is what auto_ptr is for
void TestAuto_ptr(){
std::auto_ptr cir(ShapesFactory::getInstance(CIRCLE));
cir->draw();
}
int main(){
TestFunction(); //Test implemented smart pointer
TestAuto_ptr();// Test auto_ptr
return 0;
}