求高手解释动态绑定
Sales_item():ptr(0),use(new size_t(1)){}Sales_item(Item_base& item):
ptr(item.clone()),use(new size_t(1)){}
Sales_item(const Sales_item& book):
ptr(book.ptr),use(book.use){ ++*use;}
这是Sales_item类,Item_base是基类,Bulk_item是继承Item_base的类
在这里有个一构造函数
Sales_item(Item_base& item):
ptr(item.clone()),use(new size_t(1)){}
我在主函数中使用
Sales_item item1(Bulk_item("7-115-14554-7",99,1,0.2));
Sales_item item2(Item_base("7-115-14554-8",39));
提示错误
no matching function for call to `Sales_item::Sales_item(Bulk_item)'
candidates are: Sales_item::Sales_item(const Sales_item&)
Sales_item::Sales_item(Item_base&)
Sales_item::Sales_item()
no matching function for call to `Sales_item::Sales_item(Item_base)'
candidates are: Sales_item::Sales_item(const Sales_item&)
Sales_item::Sales_item(Item_base&)
Sales_item::Sales_item()
为什么会提示没有匹配函数呢,但是如果我把
Sales_item(Item_base& item):
ptr(item.clone()),use(new size_t(1)){}
改为
Sales_item(const Item_base& item):
ptr(item.clone()),use(new size_t(1)){}
就运行正常,好像并不要求动态绑定时要用const Item_base& item吧,求高手解释下