void CalcArea() { //8
area_ = 0;
vector<Point>::iterator i; //9
for( i = points_.begin(); i != points_.end(); ++i )
area_ += /* some work */;
}
פתרון 5
void CalcArea() const {
area_ = 0;
vector<Point>::const_iterator i;
for( i = points_.begin(); i != points_.end(); ++i )
area_ += /* some work */;
}
מקרה 6
Polygon operator+( Polygon& lhs, Polygon& rhs ) { //10, 11
Polygon ret = lhs;
int last = rhs.GetNumPoints(); //12
for( int i = 0; i < last; ++i ) // concatenate
ret.AddPoint( rhs.GetPoint(i) ); //12a
return ret;
}
פתרון 6
const Polygon operator+( const Polygon& lhs, const Polygon& rhs ) {
Polygon ret = lhs;
const int last = rhs.GetNumPoints();
for( int i = 0; i < last; ++i ) // concatenate
ret.AddPoint( rhs.GetPoint(i) );
return ret;
}
מקרה 7
void f( const Polygon& poly ) { //12b
const_cast<Polygon&>(poly).AddPoint( Point(0,0) );
}
void g( Polygon& const rPoly ) { //13
rPoly.AddPoint( Point(1,1) );
}
void h( Polygon* const pPoly ) { //14
pPoly->AddPoint( Point(2,2) );
}
פתרון 7
void f(Polygon& poly ) {poly.AddPoint(Point(0,0) );}
void g(Polygon& rPoly ) { rPoly.AddPoint(Point(1,1) ); }
void h(Polygon* pPoly ) { pPoly->AddPoint(Point(2,2) ); }
תודה