TC++PL4 is now on my desk and carefully reading it I have to say that even Stroustrup makes stupid mistakes in his classes implementations.
He illustrates a typical Vector implementation (pag. 73):
class Vector {
private:
double* elem;
int sz;
public:
Vector(int s);
~Vector() { delete [] elem; }
Vector(const Vector& a);
Vector& operator=(const Vector& a);
double& operator[](int i);
const double& operator[](int i) const;
int size() const;
};
and given the fact this class needs a copy constructor implemented he "implements" it (pag. 74):
Vector::Vector(const Vector& a)
:elem{new double[sz]},
sz{a.sz}
{
for (int i = 0; i != sz; ++i)
elem[i] = a.elem[i];
}
as you can see the elem vector is built with a size retrieved from a not yet initialized variable, being it defined at page 74 I had my last hope flipping the page and checking at page 73 if "int sz" was declared before "double* elem" but it was not the case.
I'm sad.
1 comment:
mail him, put this fix to the errata.
Nice catch btw
Peter
Post a Comment