I've been reading about how to write your own copy constructors and destructors when using classes. I tried writing a simple program just to get a feel for the concepts. I created a class called "Song," and then in main(), I created a vector called allSongs which contains Song objects. Song objects are instantiated using data entered by the user (like song title, composer, etc.).
In my copy constructor and destructor, I put in this line
|
cout << "Copy constructor/destructor called\n";
|
so I would know when they were called. I was expecting to only see this message when a Song object was duplicated or deleted, but to my surprise, the message also appeared any time a new Song object was instantiated. I'm instantiating new Song objects with this line:
|
allSongs.push_back(Song(usrTitle, usrComp, usrKey, usrBpm));
|
I also noticed that those messages appear multiple times, depending on how many Song objects are in the vector. So, for example, when I push_back a third Song object to the vector, I see this:
Copy constructor called
Copy constructor called
Copy constructor called
Destructor called
Destructor called
Destructor called
So, I'm just wondering why they are being called when I'm not explicitly duplicating or deleting objects. Does it have something to do with the fact that I'm using a vector, instead of individual variables?
Thanks.