I was reading about NonVirtual Interface pattern: Herb Sutter is talking about why virtual function must be private in most of the case, protected in some case and never pub
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 typeLizard
. As a result, only theAnimal
part ofliz1
will be modified. This is a partial assignment. After the assignment,liz1
'sAnimal
members have the values they got fromliz2
, butliz1
'sLizard
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 likeAbstractAnimal
yields benefits far beyond simply making the behavior ofoperator=
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.