473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Question about copy constructor

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
1 1314
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
11
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
3
by: xllx.relient.xllx | last post by:
I have a question about an example presented in the book "INFORMIT C++ Reference Guide" by Danny Kalev: <code> string getmagicword() ( return string("Supercalifragilisticexpialidocious"); );
12
by: wickwire | last post by:
I have created a class and used it to further overload ostream: class drum { ... friend ostream& operator<< ( ostream&, drum const& ); } ostream& operator<< ( ostream& out, drum const& od )...
4
by: Jeroen | last post by:
Hi, Assume a function which returns an object of type 'my_class': my_class get_class() { ... } Now I have the following question:
19
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
3
by: MathWizard | last post by:
Hi, I have a question about returning an object in a function and calling a copy constructor. As far as I understand, in the following code the copy constructor may or may not be called,...
5
by: Vijay | last post by:
Hi All, I am not able to figure out what exactly happening in below code. what is control flow. Can anyone clear my confusion? Code: class A { public: A(){cout<<"In Constructor\n";}
8
by: hill.liu | last post by:
Hi, I stuck into this problem that I can't figure it out. Here is the class definition: class ctest { public: ctest(void) { cout << "ctest default constor" << endl; }; ctest(ctest& c) {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.