编写Basket
类
class Basket { public : void add_item ( const std:: shared_ptr< Quote> & sale) { items. insert ( sale) ; } double total_receipt ( std:: ostream& ) const ; private : static bool compare ( const std:: shared_ptr< Quote> & lhs, const std:: shared_ptr< Quote> & rhs) { return lhs-> isbn ( ) < rhs-> isbn ( ) ; } std:: multiset< std:: shared_ptr< Quote> , decltype ( compare) * > item{ compare} ; } ; double Basket :: total_receipt ( ostream & os) const { double sum = 0.0 ; for ( auto iter = items. cgegin ( ) ; iter != items. cend ( ) ; iter = items. upper_bound ( * iter) ) { sum += print_total ( os, * * iter, items. count ( * iter) ) ; } os<< "Total Sale: " << sum<< endl; return sum; }
模拟虚拟拷贝
class Quote { public : virtual Quote* clone ( ) const & { return new Quote ( * this ) ; } virtual Quote* clone ( ) && { return new Quote ( std:: move ( * this ) ) ; }
} ; class Bulk_quote : public Quote { Bulk_quote* clone ( ) const & { return new Bulk_quote ( * this ) ; } Bulk_quote* clone ( ) && { return new Bulk_quote ( std:: move ( * this ) ) ; }
} ; class Basket { public : void add_item ( const Quote& sale) { items. insert ( std:: shared_ptr < Quote> ( sale. clone ( ) ) ) ; } void add_item ( Quote&& sale) { items. insert ( std:: shared_ptr < Quote> ( std:: move ( sale) . clone ( ) ) ) ; }
} ;