//此类是抽象类–它必须内含至少一个纯虚函数,传统做法是让析构函数为纯虚函数
这技术最具意义的利益乃在于设计层面,因为将具象基类以抽象基类取而代之,可强迫你明白认知有用之抽象性质的存在。
class AbstractAnimal
{
protected://子类可以访问父类的private
AbstractAnimal& operator=(const AbstractAnimal& rhs);
public:
virtual ~AbstractAnimal() = 0;
};
class Animal :public AbstractAnimal
{
public:
Animal& operator=(const Animal& rhs);
};
class Lizard :public AbstractAnimal
{
public:
Lizard& operator=(const Lizard& rhs);
};
class Chicken :public AbstractAnimal
{
public:
Chicken& operator=(const Chicken& rhs);
};
此时允许同型赋值,部分赋值和异型赋值都在禁止之列
凡是继承此类class之具象类,都必须将该纯虚函数重新声明为一个正常的虚函数(以这题为例就是不再为0)