Prasath Shanmugakani
Prasath Shanmugakani

Reputation: 129

Anonymous constructor and destructor call in vector

Am bit confused on the below program and its output.

Could someone please clarify ?

#include <iostream>
#include <vector>

 using namespace std;
class sample
{
   public:
    int a;
    sample( ){ cout<<" Am in Default Cons "<< a << "\n"; }
    sample( int b ){ a=b; cout<<" Am in Cons "<< a << "\n"; }
    sample(const sample& s){ a=s.a; cout<<" Am in copy " <<s.a<<"\n"; }
    ~sample(){ cout<<" Am in Des "<<a <<"\n"; }

};

int main()
{
    vector < sample > Object;
    {
     Object.push_back(  sample( 1 ));
     Object.push_back(  sample( 2 ));
     cout<<" End of loop \n";
    }
    cout<<" \n End of Program \n";
    return 0; 
}

and the output is

 Am in Cons 1 // Understood this is for object 1 creation                                                                                                                                         
 Am in copy 1 // Understood this is for object 1 copy to vector 
 Am in Des 1 // Understood this is for object 1 destruction.
 Am in Cons 2  // Understood this is for object 2 creation                                                                                                                                                 
 Am in copy 2 // Understood this is for object 2 copy to vector                                                                                                                                              
 Am in copy 1 // **Don't understand this COPY CALL**                                                                                                                                              
 Am in Des 1  // **Don't understand this DESTRUCTOR CALL**                                                                                                                                               
 Am in Des 2 // Understood this is for object 2 destruction.                                                                                                                                            
 End of loop                                                                                                                                               
 End of Program                                                                                                                                            
 Am in Des 1 //// Understood this is for object 1 destruction in vector .                                                                                                                                         
 Am in Des 2 //// Understood this is for object 2 destruction in vector .                                                                                                                                         

Upvotes: 2

Views: 213

Answers (1)

Daniel Langr
Daniel Langr

Reputation: 23537

std::vector is a dynamic array. When you add an element and the capacity of its internal buffer is full, vector needs to:

  1. allocate new memory buffer,
  2. copy/move elements from old buffer to new buffer,
  3. destroy elements in original buffer,
  4. deallocate original buffer,
  5. copy/move added element at the end of new buffer.

(Not necessarily in this order.)

Step 2. involves copy constructor you do not understand. Step 3 involves destructor you do not understand.


To prevent this (inefficient) behavior, use std::vector::reserve before inserting elements (if you can). Then no reallocation and no hidden copy/move of elements will take place.

Upvotes: 9

Related Questions