Importance of Constructors while using User Defined Objects with std::vector

For User Defined classes if Copy Constructor and Assignment Operator are public then only one can insert it’s object in std::vector.

This is because of two reasons,

  • All STL contains always stores the copy of inserted objects not the actual one. So, whenever we insert any element or object in    container then it’s copy constructor is called to create a copy and then this copy is inserted in the container.
  • While insertion in std::vector it might be possible that storage relocation takes place internally due to insufficient space. In such cases assignment operator will be called on objects inside the container to copy them from one location to another.

Check out below example,

class Sample
{
	// Copy Constructor should not be private if we are inserting it's
	// objects in std::vector
	Sample(const Sample & obj)
	{
		std::cout<<"Sample :: Copy Constructor"<<std::endl;
	}
public:
	Sample()
	{
		std::cout<<"Sample :: Default Constructor"<<std::endl;
	}

	Sample & operator=(const Sample & obj)
	{
		std::cout<<"Sample :: Assignment Operator"<<std::endl;
	}
};

int main()
{
	std::vector<Sample> vecOfSamples;
	Sample obj;
	vecOfSamples.push_back(obj);

	return 0;
}

A part from this, if you are initializing the std::vector with default size by passing size as parameter i.e.

std::vector<NewSample> vecOfNewSample(2);

In such scenario 2 objects of NewSample will be created using default constructor. But if default constructor is not available then it will give compile error.

[showads ad=inside_post]

Check out the following example,

class NewSample
{
public:
	// Remove these comments to make it compilable
	NewSample()
	{}
	NewSample(const NewSample & obj)
	{}
	NewSample & operator=(const NewSample & obj)
	{}
};

int main()
{

	std::vector<NewSample> vecOfNewSample(2);
	// It will create 2 Objects using default constructor and insert them in vector.

	std::cout<<"vecOfNewSample Size = "<<vecOfNewSample.size()<<std::endl;
	std::cout<<"vecOfNewSample Capacity = "<<vecOfNewSample.capacity()<<std::endl;

	std::vector<NewSample> vecOfNewSample_2;
	vecOfNewSample_2.reserve(2);
	// It will make the capacity of vector to contain 2 objects.
	// ALthough size will remain 2.

	std::cout<<"vecOfNewSample_2 Size = "<<vecOfNewSample_2.size()<<std::endl;
	std::cout<<"vecOfNewSample_2 Capacity = "<<vecOfNewSample_2.capacity()<<std::endl;

	return 0;
}

Here we have not Defined Default constructor but defined copy constructor, therefore default constructor will get hidden for this class. Hence, it will give compile error when vector will try to create two objects using default constructor.

This problem will not happen if we use reserver() member function to initialize the size of vector because reserver() just increases the capacity of vector not the size.

1 thought on “Importance of Constructors while using User Defined Objects with std::vector”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top