关于c++ primer的一点问题

lvjing_CSDN 2011-08-25 04:49:17
我在看c++ primer的564页的一段话,我觉得讲的不对,但我又不太敢确定,望各位大侠指教。先把代码贴下:

class Item_base
{
public:
Item_base(const string &book ="",double sales_price = 0.0):isbn(book),price(sales_price){}
string book() const { return isbn;}
virtual double net_price (size_t n) const {return n * price;}
virtual ~Item_base(){}
virtual Item_base *clone() const
{ return new Item_base(*this); }

private:
string isbn;
protected:
double price;
};


class Bulk_item: public Item_base
{
public:
Bulk_item():min_qty(0),discount(0.0){}
Bulk_item(const string &book,double sales_price,size_t qty=0, double disc_rate = 0.0):
Item_base(book,sales_price), min_qty ( qty), discount(disc_rate){}
double net_price(size_t) const;
virtual Bulk_item *clone() const
{ return new Bulk_item(*this); }

private:
size_t min_qty;
double discount;
};
double Bulk_item::net_price(size_t cnt) const
{
return cnt >=min_qty ? cnt * (1 - discount)*price : cnt * price;
}


void print_total (ostream &os, const Item_base &item, size_t n)
{
os << "ISBN: " << item.book() << "\tnumber sold: " << n <<"\ttotal price:"
<< item.net_price(n) << endl;
}
class Sales_item
{
public:
Sales_item():h( ){}
Sales_item(const Item_base &item):h(item.clone()){}
const Item_base &operator*() const { return *h; }
const Item_base *operator->() const { return h.operator->();}
private:
Handle<Item_base> h;
};

inline bool compare(const Sales_item & lhs, const Sales_item &rhs)
{
return lhs->book() < rhs->book();
}

class Basket
{
typedef bool (* Comp) (const Sales_item &,const Sales_item &);
public:
typedef multiset<Sales_item,Comp> set_type;
typedef set_type::size_type size_type;
typedef set_type::const_iterator const_iter;
Basket(): items(compare){}
void add_item(const Sales_item &item)
{ items.insert(item); }
size_type size(const Sales_item & i) const
{return items.count(i);}
double total()const;
private:
set_type items;
};

double Basket::total()const
{
double sum=0;
for (const_iter iter=items.begin();iter != items.end(); iter=items.upper_bound(*iter))
sum+=(*iter)->net_price(items.count(*iter));
return sum;
}

关于total函数中sum+=(*iter)->net_price(items.count(*iter));的分析如下(书上的分析):
1(*item) 返回h , h 是使用计数式句柄的成员。
2因此, (*item)->使用句柄类的重载箭头操作符。
3编译器计算h.operator->() ,获得该Handle 对象保存的工terrLbase 指针。
4编译器对该工tem_base 指针解引用,并调用指针所指对象的netJrice 成员。

我觉得前两点不太对
1(*item)返回的是Salse_item对象。
2 (*item)->使用的是Salse_item对象重载的箭头操作符。

望各位大侠和也看过这个地方的同学能多多指教。
...全文
174 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvjing_CSDN 2011-08-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maoxing63570 的回复:]

We dereference iter to get the underlying Sales_item to which we apply the overloaded arrow
operator from the Sales_item class. That operator returns the underlying Item_base object to
which the ha……
[/Quote]
你是在那一页找到的这段话,我也有英文版,但没注意这段在哪。那这段话的意思是不是也赞成我的意见呢?
lvjing_CSDN 2011-08-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mervynhit 的回复:]

typedef multiset<Sales_item,Comp> set_type;
set_type被定义为一个sales_item的集合
所以(*item)为Sales_item的*重载,返回
Handle<Item_base> h;
所以(*item)->为Handle的->重载
[/Quote]
item是一个迭代器,我觉得对其解引用得到的是set的中的元素
lvjing_CSDN 2011-08-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ljljlj 的回复:]

第三版还是第四版?lippman写的那本吗?564页怎么没有?
我看的是电子版,请问是第几章第几节?
[/Quote]
谢谢你的回复,我看的是第四版的564上方的一段话。
yah606060 2011-08-25
  • 打赏
  • 举报
回复
如果 ptr 对象所属的类重载了 -> 操作符,那么

ptr->a

等价为

(ptr.operator->())->a
编译器还是帮我们做了 ->a
mervynhit 2011-08-25
  • 打赏
  • 举报
回复
typedef multiset<Sales_item,Comp> set_type;
set_type被定义为一个sales_item的集合
所以(*item)为Sales_item的*重载,返回
Handle<Item_base> h;
所以(*item)->为Handle的->重载
maoxing63570 2011-08-25
  • 打赏
  • 举报
回复
We dereference iter to get the underlying Sales_item to which we apply the overloaded arrow
operator from the Sales_item class. That operator returns the underlying Item_base object to
which the handle is attached. From that object we call net_price , passing the count of items
with the same isbn . The net_price function is virtual, so the version of the pricing function that
is called depends on the type of the underlying Item_base object.


quote:c++ primer fourth edition
ljhhh0123 2011-08-25
  • 打赏
  • 举报
回复
第三版还是第四版?lippman写的那本吗?564页怎么没有?
我看的是电子版,请问是第几章第几节?
C++ Primer, Fourth Edition, provides a comprehensive introduction to the C++ language. As a primer, it provides a clear tutorial approach to the language, enhanced by numerous examples and other learning aids. Unlike most primers, it also provides a detailed description of the language, with particular emphasis on current and effective programming techniques. 本书全面介绍了 C++ 语言。作为一本入门书(Primer),它以教程的形式对 C++ 语言进行清晰的讲解,并辅以丰富的示例和各种学习辅助手段。与大多数入门教程不同,本书对 C++ 语言本身进行了详尽的描述,并特别着重介绍了目前通行的、行之有效的程序设计技巧。 Countless programmers have used previous editions of C++ Primer to learn C++. In that time C++ has matured greatly. Over the years, the focus of the languageand of C++ programmershas grown beyond a concentration on run-time efficiency to focus on ways of making programmers more efficient. With the widespread availability of the standard library, it is possible to use and learn C++ more effectively than in the past. This revision of the C++ Primer reflects these new possiblities. 无数程序员曾使用本书的前几个版本学习 C++,在此期间 C++ 也逐渐发展成熟。这些年来,C++ 语言的发展方向以及 C++ 程序员的关注点,已经从以往注重运行时的效率,转到千方百计地提高程序员的编程效率上。随着标准库的广泛可用,我们现在能够比以往任何时候更高效地学习和使用 C++。本书这一版本充分体现了这一点

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧