| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 786 人关注过本帖
标题:大家帮我解释一下C++句柄指针类
只看楼主 加入收藏
specilize
Rank: 4
等 级:业余侠客
帖 子:126
专家分:247
注 册:2011-2-20
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
大家帮我解释一下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; } }
};
搜索更多相关主题的帖子: return 源代码 
2011-04-22 19:05
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:10 
因为句柄不等同指针。自己百度下智能指针。

迭代的是人,递归的是神。
2011-04-24 09:13
specilize
Rank: 4
等 级:业余侠客
帖 子:126
专家分:247
注 册:2011-2-20
收藏
得分:0 
回复 2楼 lintaoyn
明白了,但是为什么我返回this时编译会提示错误(就像我那样做),而像他那样clone时就不会呢,我觉得语法上应该是可以的啊
2011-04-24 11:27
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:0 
{ return this); }
括号不匹配吧。要自己学着看编译器报的错误

迭代的是人,递归的是神。
2011-04-24 11:52
快速回复:大家帮我解释一下C++句柄指针类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047301 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved