public:
std::pair<size_t, double> discount_policy() const
{ return std::make_pair(quantity, discount); }
// other members as before
};
We can access discount_policy only through an object, pointer, or reference of type Disc_item or a class derived from Disc_item:(只有Disc_item和它的派生类的对象,指针和引用可以访问discount_policy()函数)
Bulk_item bulk;
Bulk_item *bulkP = &bulk; // ok: static and dynamic types are the same
Item_base *itemP = &bulk; // ok: static and dynamic types differ
bulkP->discount_policy(); // ok: bulkP has type Bulk_item*
itemP->discount_policy(); // error: itemP has type Item_base*
The call through itemP is an error because a pointer (reference or object) to a base type can access only the base parts of an object and there is no discount_policy member defined in the base class.(itemP是一个Item_base类型指针,它只能访问Item_base部分,不能访问Disc_item特有的部分)
6228

被折叠的 条评论
为什么被折叠?



