Question about copy constructor



 DEVELOP > c-Plus-Plus > Question about copy constructor

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "fl"
Date: 15 Jan 2008 10:17:25 AM
Object: Question about copy constructor
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, Comp> items;
};
....
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:
...
};
.

User: "Victor Bazarov"

Title: Re: Question about copy constructor 15 Jan 2008 10:22:38 AM
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, Comp> items;
};
...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER