We know vector can store homogeneous kind of stuff. That is, it can store type int, string etc individually but not element of type int with string etc.
However, sometimes we might have requirements that it can store elements which are in inheritance relation and can do stuff by calling function of respective element stored in vector. As an example, I've a Shape class from which I've derived Circle, Polygon etc. Now I want to store Circle, Polygon object in a vector and then access each element by its iterator and draw shape accordingly.
C++ 2011 made it possible via shared_ptr. Thanks to Stephan aka STL.
Code snippet
#include < iostream >
#include < memory >
#include < vector >
using namespace std;
class Shape
{
public:
Shape(){}
virtual ~Shape(){}
virtual void draw(int x, int y)
{
cout << "Shape::draw(" << x << ", " << y << ")" << endl;
}
private:
Shape(const Shape&);
Shape& operator=(const Shape&);
};
class Circle : public Shape
{
public:
virtual void draw(int x, int y) override
{
cout << "Circle::draw(" << x << ", " << y << ")" << endl;
}
};
class Polygon : public Shape
{
public:
virtual void draw(int x, int y)
{
cout << "Polygon::draw(" << x << ", " << y << ")" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector< shared_ptr< Shape > >vec;
shared_ptr< Shape > shape = make_shared< Polygon >();
vec.push_back(shape);
shape = make_shared< Circle >();
vec.push_back(shape);
vec.at(0)->draw(11, 22);
vec.at(1)->draw(33, 44);
return 0;
}
I've used Visual Studio 2010 to build this code. And output will be:
Polygon::draw(11, 22)
Circle::draw(33, 44)
However, sometimes we might have requirements that it can store elements which are in inheritance relation and can do stuff by calling function of respective element stored in vector. As an example, I've a Shape class from which I've derived Circle, Polygon etc. Now I want to store Circle, Polygon object in a vector and then access each element by its iterator and draw shape accordingly.
C++ 2011 made it possible via shared_ptr. Thanks to Stephan aka STL.
Code snippet
#include < iostream >
#include < memory >
#include < vector >
using namespace std;
class Shape
{
public:
Shape(){}
virtual ~Shape(){}
virtual void draw(int x, int y)
{
cout << "Shape::draw(" << x << ", " << y << ")" << endl;
}
private:
Shape(const Shape&);
Shape& operator=(const Shape&);
};
class Circle : public Shape
{
public:
virtual void draw(int x, int y) override
{
cout << "Circle::draw(" << x << ", " << y << ")" << endl;
}
};
class Polygon : public Shape
{
public:
virtual void draw(int x, int y)
{
cout << "Polygon::draw(" << x << ", " << y << ")" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector< shared_ptr< Shape > >vec;
shared_ptr< Shape > shape = make_shared< Polygon >();
vec.push_back(shape);
shape = make_shared< Circle >();
vec.push_back(shape);
vec.at(0)->draw(11, 22);
vec.at(1)->draw(33, 44);
return 0;
}
I've used Visual Studio 2010 to build this code. And output will be:
Polygon::draw(11, 22)
Circle::draw(33, 44)
Comments