On Jun 12, 10:59 pm, Ramon F Herrera <ra...@conexus.netwrote:
Newbie alert: I come from C programming, so I still have that frame of
mind, but I am trying to "Think in C++". In C this problem would be
solved using unions.
Hello:
Please consider the snippet below. I have some objects which are
derived (subclassed?, subtyped?) from simpler ones, in increasing
size. There is a linear hierarchy. I need to keep them all in some
sort of linked list (perhaps std::vector). I could have several
vectors, one for every different class, but I have an intuitive belief
that C++ must be able to provide a means to save all the objects in
the same vector.
You certainly can, assuming you design a hierarchy that supports a
base type.
Think 'Shapes'
>
Issue #1: Notice the definition of the vector. Should I use the
largest class (vector<pentagon>) to reserve space for the extreme
case? Or, should I define a vector<triangleinstead since the others
are inheritors of the triangle class?
Maybe my approach is wrong to begin with?
TIA,
-RFH
Consider the following, lets forget the coordinate system for now
#include <iostream>
#include <vector>
class Shape { };
class Triangle : public Shape { };
class Rectangle : public Shape { };
class Circle : public Shape { };
int main()
{
std::vector< Shape* shapes;
Triangle tri;
Rectangle rect;
Circle circ;
shapes.push_back(&tri);
shapes.push_back(&rect);
shapes.push_back(&circ);
std::cout << "vector shapes has ";
std::cout << shapes.size() << " elements\n";
}
>
-------------------
#include <vector>
using namespace std;
struct point
{
int x, y;
};
class triangle
{
public :
point vertex1;
point vertex2;
point vertex3;
};
class square : public triangle
{
public :
point vertex4;
};
class pentagon : public square
{
public :
point vertex5;
};
int main()
{
vector<triangle piggybank;
triangle *love_affair = new triangle;
square *boxing_ring = new square;
pentagon *military = new pentagon;
military->vertex5.x = 123;
military->vertex5.y = 456;
piggybank.push_back(*military);
piggybank.push_back(*boxing_ring);
piggybank.push_back(*love_affair);
return 0;
}
I'ld suggest choosing a smart_pointer instead of new.
You would end up with something like the following (using
boost::shared_ptr in this case)
#include <iostream>
#include <vector>
#include "boost/smart_ptr.hpp"
class Shape { };
class Triangle : public Shape
{
public:
~Triangle(){ std::cout << "~Triangle\n"; }
};
class Rectangle : public Shape
{
public:
~Rectangle(){ std::cout << "~Rectangle\n"; }
};
class Circle : public Shape
{
public:
~Circle(){ std::cout << "~Circle\n"; }
};
int main()
{
std::vector< boost::shared_ptr<Shape shapes;
// Triangle tri;
// Rectangle rect;
// Circle circ;
shapes.push_back(boost::shared_ptr<Shape>(new Triangle));
shapes.push_back(boost::shared_ptr<Shape>(new Rectangle));
shapes.push_back(boost::shared_ptr<Shape>(new Circle));
std::cout << "vector shapes has ";
std::cout << shapes.size() << " elements\n";
}
/*
vector shapes has 3 elements
~Triangle
~Rectangle
~Circle
*/
The above uses default constructors only, if parametized ctors are
supplied, the concept remains the same.