Vector of class objects in C++

Last Updated :

Vector usually deals with primitive data types like int, string, double. However, there would be complexities and overhead arising when dealing with vector of class objects.

For example, what will be output of below C++ program.

#include <bits/stdc++.h> 
using namespace std; 

// class x
class x{
  int i;
  public:
  x(){
    cout<<"constructor"<<'\n';
    i = 1;
  }
  x(const x &p2){
    cout<<"copy constructor"<<'\n';
  }
  ~x(){
    cout<<"destructor"<<'\n';
  }
};

// driver program
int main() 
{ 
       // vector
	vector<x> v; 
  	v.reserve(7);

	x obj;
   for(unsigned i=0;i<6;i++){
     v.push_back(obj);
   }
   return 0; 
}

How many constructors will be called here?
How many constructors will be called?
How many destructors will be called?

The output of above program is :

constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor 

There will be one constructor when we define object obj of class x. 7 copy constructors when we push_back object into the vector and 8 destructors, one for obj and 7 for class x objects pushed into the vector.

Now let us tweak the program a bit and see what is the output:

#include <bits/stdc++.h> 
using namespace std;

// class x 
class x{
  int i;
  public:
  x(){
    cout<<"constructor"<<'\n';
    i = 1;
  }
  x(const x &p2){
    cout<<"copy constructor"<<'\n';
  }
  ~x(){
    cout<<"destructor"<<'\n';
  }
};

// driver program
int main() 
{ 
        // vector
	vector<x> v; 
  	v.reserve(7);

	x obj;
   for(unsigned i=0;i<8;i++){
     v.push_back(obj);
   }
   return 0; 
}

The output of above program is:

constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
copy constructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor
destructor

Since we reserved space for only 8 objects, when the 8th object is copied, OS needed to allocate bigger storage somewhere else.

So, it has to copy the already existing objects from older storage to new storage, thus calling copy constructors. Allocate storage for new object to be pushed back into vector. Now, here comes releasing the old storage thus calling 16 destructors. Finally it calls 16 destructors when the program terminates.

Note :

  1. When the vector runs out of its reserved space by user, it does not throw error. It allocates bigger storage somewhere.
  2. Copy existing elements from old storage to new storage.
  3. Appends new element to the vector.
  4. Releases memory allocated to old storage.

Thus in a class when you are keeping count of how many objects of a class is created using some static variable in its copy constructor, do not get confused by this behavior when you are using vector of that class objects.



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next Article
CPP_Programmer Published 10 Aug, 2020 · 3 min read
Article Tags :

Similar Reads

  • Four Pillars of Object-Oriented Programming (OOPS)

    Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples. What is OOP? Picture a cluttered garage🛠️. Tools are scattered everywhere,…

    4 min read
  • What is Overriding in C++

    Write a program in C++ to depict the concept of overriding or Dynamic polymorphism. Overriding is a type of polymorphism. Polymorphism in OOP languages means to take more…

    2 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • What is Object-Oriented Programming

    OOPs stands for Object Oriented Programming. OOP is a programming style with Classes, Objects, Inheritance, Polymorphism, Encapsulation, Abstraction. Before OOP, Procedural Programming like C was used, based on…

    2 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read