????case 5:
????????????????
????????????????????????????????????????????????????????????????λ??????
???????????′???:
????Point x;
????Point y(x);
????????????????Point???? x??y?? m_color???????"Red"?????;
????????????????????????????? m_color????????????????3?????;
?????????????
??????д??????????????;
????????Point????д???????£?
????Point::Point(const Point& y)
????: m_x(y.m_x)?? m_y(y.m_y)
????{
????m_color = new char[strlen(y.m_color) + 1];
????::strcpy(m_color?? y.m_color);
????}
????case 6:
??????????????????????????????????
???????????????????????
????1> ?????????????????????????????????????????????
????Point x(x); // ????;
????????????????????;
????Point x = x; // ????????????????????????!
???????????????????????????????x????????棬??????????????????m_color????????;
???????飬???????????????????????????????????????????????;
????2> ???????????????????????;
????3> ???????????????new????????????(?????????????????????????????????????????????????)
????????????????£?
????const Point& Point::operator =(const Point& rhs)
????{
????// ?????????????
????// ??????ü?????????????????????????COM????????????????????????;
????if (this != &rhs)
????{
????m_x = rhs.m_x;
????m_y = rhs.m_y;
????// ????????????;
????// ?????μ?;
????delete m_color;
????m_color = new char[strlen(rhs.m_color) + 1];
????strcpy(m_color?? rhs.m_color);
????}
????return *this;
????}
???????????const???????????????????????????????????
????(x = y) = z;
????????????????????????????????????£???????const???;
????case 7:
????????nonmodifying?????????????????;
??????νnonmodifying?????????????????????????????????????????????????????????;
?????????????????????????????bool??;
?????????????????(=?? += ??<<=???);
??????????????????????????浽?????????????棬???????????Ч???????????(&);
???????????:
????1> ????static?? ???????????????????洢???;
??????????????ú????????? w = x+y+z;
for example:
// case 7?????????1??static
const Point& Point::operator +(const Point& rhs) const
{
static Point temp;
temp.m_x = this->m_x + rhs.m_x;
temp.m_y = this->m_y + rhs.m_y;
// ?????????????;
delete temp.m_color;
temp.m_color = new char[strlen(this->m_color) + strlen(rhs.m_color) + 1];
sprintf(temp.m_color?? "%s%s"?? this->m_color?? rhs.m_color);
return temp;
}
?????????????????????п????????????????ж??????????????????????????????????????Effective c++ Item 19??
????2> ????????????????;(?e??????????Ч??)
???????????????????????????????????:
Point *temp = new Point;
...
return (*temp);
????????????????????????λ???????????????й?;
const Point Point::operator +(const Point& rhs) const
{
Point temp;
temp.m_x = this->m_x + rhs.m_x;
temp.m_y = this->m_y + rhs.m_y;
// ?????????????;
delete temp.m_color;
temp.m_color = new char[strlen(this->m_color) + strlen(rhs.m_color) + 1];
sprintf(temp.m_color?? "%s%s"?? this->m_color?? rhs.m_color);
return temp;
}
????case 8:
??????y????????????????????麯??;
???????????:
?????????????????????????麯??;
????????????????????????????:
????????????????????;
for example:
Apple is a kind of fruit?? and banana also is;
so someone write such codes:
Fruit *basket[20];
for (int i = 0; i < 10; ++i)
{
basket[i] = new Apple;
// ??????????;
...
}
for (; i < 20; ++i)
{
basket[i] = new Banana;
// ?????????;
...
}
// ???Fruitde?????????????麯????????????????(???Apple??Banana???????????new?????????)
for (i = 0; i < 20; ++i)
{
delete basket[i];
}
????????????!
???????
????1> ???????????????????????????????μ?new????????????????????;?????????????????
??????????????????????麯???????????????;
????2> ???費(fèi)???麯?????????????????????????????????????????????????????????;
????3> ??????????麯??????????????????????????????????????????????;
????4> ??????麯???????????????????????????????????????????????????????????????????????????!