大家帮我解释一下C++句柄指针类
以下是C++primer第四版中的源代码,我在要问的地方添加注释基类
class Item_base {
friend std::istream& operator>>(std::istream&, Item_base&);
friend std::ostream& operator<<(std::ostream&, const Item_base&);
public:
virtual Item_base* clone() const
{ return new Item_base(*this); }
//我想问的就是这地方(以下派生类相同),为什么不能为
virtual Item_base* clone() const
{ return this); }
为什么一定要返回*this的副本的指针,而不用this去初始化化Sales_item中的指向基类的指针
public:
Item_base(const std::string &book = "",
double sales_price = 0.0):
isbn(book), price(sales_price) { }
std::string book() const { return isbn; }
// returns total sales price for a specified number of items
// derived classes will override and apply different discount algorithms
virtual double net_price(std::size_t n) const
{ return n * price; }
// no work, but virtual destructor needed
// if base pointer that points to a derived object is ever deleted
virtual ~Item_base() { }
private:
std::string isbn; // identifier for the item
protected:
double price; // normal, undiscounted price
};
以下是派生类
class Bulk_item : public Item_base {
public:
std::pair<size_t, double> discount_policy() const
{ return std::make_pair(min_qty, discount); }
// other members as before
Bulk_item* clone() const
{ return new Bulk_item(*this); }
Bulk_item(): min_qty(0), discount(0.0) { }
Bulk_item(const std::string& book, double sales_price,
std::size_t qty = 0, double disc_rate = 0.0):
Item_base(book, sales_price),
min_qty(qty), discount(disc_rate) { }
// redefines base version so as to implement bulk purchase discount policy
double net_price(std::size_t) const;
private:
std::size_t min_qty; // minimum purchase for discount to apply
double discount; // fractional discount to apply
};
// discount (a fraction off list) for only a specified number of copies,
// additional copies sold at standard price
class Lim_item : public Item_base {
public:
Lim_item(const std::string& book = "",
double sales_price = 0.0,
std::size_t qty = 0, double disc_rate = 0.0):
Item_base(book, sales_price),
max_qty(qty), discount(disc_rate) { }
// redefines base version so as to implement limited discount policy
double net_price(std::size_t) const;
private:
std::size_t max_qty; // maximum number sold at discount
double discount; // fractional discount to apply
public:
Lim_item* clone() const { return new Lim_item(*this); }
std::pair<size_t, double> discount_policy() const
{ return std::make_pair(max_qty, discount); }
};
句柄指针类
class Sales_item {
friend class Basket;
public:
// default constructor: unbound handle
Sales_item(): p(0), use(new std::size_t(1)) { }
// attaches a handle to a copy of the Item_base object
Sales_item(const Item_base&);
// copy control members to manage the use count and pointers
Sales_item(const Sales_item &i):
p(i.p), use(i.use) { ++*use; }
~Sales_item() { decr_use(); }
Sales_item& operator=(const Sales_item&);
const Item_base *operator->() const { if (p) return p;
else throw std::logic_error("unbound Sales_item"); }
const Item_base &operator*() const { if (p) return *p;
else throw std::logic_error("unbound Sales_item"); }
private:
Item_base *p; // pointer to shared item
std::size_t *use; // pointer to shared use count
void decr_use()
{ if (--*use == 0) { delete p; delete use; } }
};