tfmccarthy_verizon_mail
unread,Apr 26, 2012, 2:49:39 AM4/26/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ppp-p...@googlegroups.com
Was anyone else bothered by this,
pp 460, "We thought of using a vector<Rectangle>, but that tuned out to
be not quite flexible enough."
The is the justification for the Vector_ref class and it seemed just wrong.
If you try to use vector<Rectangle> it will fail, so IMHO, flexibility isn't
the problem. This makes it seem like vector is deficient in some way and
that isn't so. It seemed to me that Vector_ref was attacking the wrong
problem. The problem is in the base class Shape and that is where the
solution focus should be.
With that in mind I worked up the following to display the color chart
example. This just seems cleaner and less complex to me. Comments?
//
// This is example code from Chapter 13.10 "Managing unnamed objects" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
#include "Simple_window.h" // get access to our window library
#include "Graph.h" // get access to our graphics library facilities
//------------------------------------------------------------------------------
// RectangleEx
// This class allows the use a vector to hold Rectangles. It extends the
// Rectangle class and provides the required interface to support a vector.
struct RectangleEx : Rectangle
{
public:
RectangleEx(Point xy, int ww, int hh) // Rectangle constructor
: Rectangle(xy, ww, hh)
{
}
RectangleEx(Point x, Point y) // Rectangle constructor
: Rectangle(x, y)
{
}
RectangleEx(const RectangleEx& rhs) // copy constructor
: Rectangle(rhs.point(0), rhs.width(), rhs.height())
{
*this = rhs;
}
RectangleEx& operator = (const RectangleEx& rhs) // assignment
{
if (this == &rhs) {
return *this;
}
set_color(rhs.color());
set_style(rhs.style());
set_fill_color(rhs.fill_color());
return *this;
}
void draw_lines() const // needed by Shape
{
Rectangle::draw_lines();
}
protected:
RectangleEx() // default constructor (unused)
: Rectangle(Point(0,0), 1, 1)
{
}
};
int main()
try
{
using namespace Graph_lib; // our graphics facilities are in Graph_lib
Simple_window win(Point(100,100),600,400,"16*16 color matrix");
vector <RectangleEx> colorchart;
const int MAX_ROWS = 16;
const int MAX_COLS = 16;
for (int i = 0; i < MAX_ROWS; ++i) {
for (int j = 0; j < MAX_COLS; ++j) {
colorchart.push_back(RectangleEx(Point(i * 20, j * 20), 20, 20));
colorchart[colorchart.size()-1].set_fill_color(i * 16 + j);
}
}
// must defer attachement of colorchart elements until fully constructed.
// This is due to how vector performs memory expansion
for (int i = 0; i < MAX_ROWS; ++i) {
for (int j = 0; j < MAX_COLS; ++j) {
win.attach(colorchart[(i * MAX_ROWS) + j]);
}
}
win.wait_for_button(); // Display!
}
catch(exception& e) {
// some error reporting
cerr << "Opps! error " << e.what() << endl;
return 1;
}
catch(...) {
// some more error reporting
cerr << "Opps! Something bad happened" << endl;
return 2;
}
//------------------------------------------------------------------------------