本文共 1027 字,大约阅读时间需要 3 分钟。
15.5.1. Name Lookup Happens at CompileTime
The static type of an object, reference, or pointer determinesthe actions that the object can perform. Even whenthe static and dynamic types might differ, as can happen when a reference orpointer to a base type is used, the static type determines what members can beused. As an example, we might add a member to the Disc_item class that returns a pair holding the minimum (or maximum) quantity and the discounted price:
class Disc_item : public Item_base {
public:
std::pair<size_t, double>discount_policy() const
{ return std::make_pair(quantity,discount); }
// other members as before
};
Wecan access discount_policyonly through an object, pointer, or reference of typeDisc_item or a class derived from Disc_item:
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*
转载地址:http://xyyii.baihongyu.com/