本文共 917 字,大约阅读时间需要 3 分钟。
15.6. Pure Virtual Functions
A pure virtual function is specified by writing = 0 after the function parameter list:
class Disc_item : public Item_base {
public:
double net_price(std::size_t) const =0;
};
Defining a virtual as pure indicates that the function provides an interface for sub-sequent types to override but that the version in this class will never be called. As importantly, users will not be able to create objects of type Disc_item.
An attempt to create an object of an abstract base class is a compile-time error:
// Disc_item declares pure virtual functions
Disc_item discounted; // error: can't define a Disc_item object
Bulk_item bulk; // ok: Disc_item subobject within Bulk_item
Note:A class containing (or inheriting) one or more pure virtual functions is an abstract base class . We may not create objects of an abstract type except as parts of objects of classes derived from the abstract base.
转载地址:http://myyii.baihongyu.com/