By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,908 Members | 2,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,908 IT Pros & Developers. It's quick & easy.

Question about copy constructor

P: n/a
fl
Hi,
I browse through C++ primer. I have a question about the calling of
copy constructor in the example of that book. Below, "item" is an
associate container. In main func, there is a call of add_item. From
debug, I see add_item calls "Sales_item(const Sales_item &i)". My
question is: why add_item calls the copy constructor. To you, it may
be very easy. For a beginner like me, I don't know now. Could you give
me an explanation about that? Thanks in advance.

..........................
class Basket {
....
public:
....
void add_item(const Sales_item &item)
{items.insert(item); }

private:
std::multiset<Sales_item, Compitems;
};
....
class Sales_item {
friend class Basket;
public:
....
// copy control members to manage the use count and pointers
Sales_item(const Sales_item &i):
p(i.p), use(i.use) { ++*use; }

private:
...
};
Jan 15 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
fl wrote:
Hi,
I browse through C++ primer. I have a question about the calling of
copy constructor in the example of that book. Below, "item" is an
associate container. In main func, there is a call of add_item. From
debug, I see add_item calls "Sales_item(const Sales_item &i)". My
question is: why add_item calls the copy constructor. To you, it may
be very easy. For a beginner like me, I don't know now. Could you give
me an explanation about that? Thanks in advance.

.........................
class Basket {
...
public:
...
void add_item(const Sales_item &item)
{items.insert(item); }
'std::multiset' makes copies of things that you insert. That's
just how all standard containers work.
>
private:
std::multiset<Sales_item, Compitems;
};
...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 15 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.