13.10 Managing unnamed objects

15 views
Skip to first unread message

tfmccarthy_verizon_mail

unread,
Apr 26, 2012, 2:49:39 AM4/26/12
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;
}

//------------------------------------------------------------------------------


tfmccarthy_verizon_mail

unread,
Apr 27, 2012, 9:27:05 PM4/27/12
to ppp-p...@googlegroups.com
Fail!

{
RectangleEx rect1(Point(0,0), 10, 10); // 10 x 10
RectangleEx rect2(Point(0,0), 20, 20); // 20 x 20
rect1 = rect2;
if (rect1.height() != rect2.height()
|| rect1.width() != rect2.width()) {
cerr << "Error!: RectangleEx assignment fails!" << endl;
> --
> You received this message because you are subscribed to the Google Groups
> "PPP-public" group.
> To post to this group, send email to ppp-p...@googlegroups.com.
> To unsubscribe from this group, send email to
> ppp-public+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/ppp-public?hl=en.
>


Reply all
Reply to author
Forward
0 new messages