You could buy a copy of More Effective C++, or check your local library for a copy, and read item 33 for the full explanation. The explanation given there is that it makes your class prone to partial assignment, also known as slicing:
There are two problems here. First, the assignment operator invoked on the last line is that of the Animal
class, even though the objects involved are of type Lizard
. As a result, only the Animal
part of liz1
will be modified. This is a partial assignment. After the assignment, liz1
's Animal
members have the values they got from liz2
, but liz1
's Lizard
members remain unchanged.
?????The second problem is that real programmers write code like this. It's not uncommon to make assignments to objects through pointers, especially for experienced C programmers who have moved to C++. That being the case, we'd like to make the assignment behave in a more reasonable fashion. As Item 32 points out, our classes should be easy to use correctly and difficult to use incorrectly, and the classes in the hierarchy above are easy to use incorrectly.
See this question and others for a description of the object slicing problem in C++.
Item 33 also says this, later on:
Replacement of a concrete base class like Animal
with an abstract base class like AbstractAnimal
yields benefits far beyond simply making the behavior of operator=
easier to understand. It also reduces the chances that you'll try to treat arrays polymorphically, the unpleasant consequences of which are examined in Item 3. The most significant benefit of the technique, however, occurs at the design level, because replacing concrete base classes with abstract base classes forces you to explicitly recognize the existence of useful abstractions. That is, it makes you create new abstract classes for useful concepts, even if you aren't aware of the fact that the useful concepts exist.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…