fl <
rxj...@gmail.com> writes:
Replying to your own followup so everything is double quoted.
> On Tuesday, October 6, 2015 at 9:33:29 AM UTC-4, fl wrote:
<snip>
>> void Basket::display(ostream &os) const
>> {
<snip>
>> for (const_iter next_item = items.begin();
>> next_item != items.end();
>> next_item = items.upper_bound(*next_item))
>> {
>> // we know there's at least one element with this key in the Basket
>> os << (*next_item)->book() << " occurs "
items is a multiset of Sales_item, so *next_item is if type const Sales_item.
<snip>
>> << endl;
>> }
>> }
<snip>
>> class Item_base {
<snip>
>> std::string book() const { return isbn; }
<snip>
>> };
>>
>> class Sales_item {
>> friend class Basket;
>> public:
<snip>
>> const Item_base *operator->() const { if (p) return p;
>> else throw std::logic_error("unbound Sales_item"); }
This operator is the key.
<snip>
>> };
<snip>
>> // holds items being purchased
>> class Basket {
<snip>
>> private:
>> std::multiset<Sales_item, Comp> items;
>> };
>>
>> book() is Item_base' member function. Why can (*next_item)->book() be
>> used?
So, *next_item is if type const Sales_item but Sales_item defines
operator-> so an expression of the form x->m is interpreted as
(x.operator->())->m for a class object x. I.e. (*next_item)->book()
means
((*next_item).operator->())->book();
Do you see it now?
<snip>
--
Ben.