-1

https://www.educative.io/edpresso/what-is-a-move-constructor-in-cpp

int main() 
{    
  vector <A> vec;
  vec.push_back(A());

  return 0;    
}

Now, when the above code is executed, the default constructor is called at the time that the temporary object A is created. The copy constructor is called as the temporary object of A is pushed back in the vector.

All I know is that copy constructor gets called like this:

classA obj1;
classA obj2 = obj1;

Why does copy constructor get called with this statement?
vec.push_back( A() );

Here all we are doing is calling the constructor with A() and then the object will get stored in the vector.

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

4 Answers4

3

...and then the object will get stored in the vector.

That is exactly where the copy constructor is called. Since the vector stores all its values in a contiguous array and A() object is definetily not in such array already, it must be either copied or moved to its place.

This is also exactly the reason why STL containers offer emplace_ functions that take constructor arguments instead and create the actual object only at its final place in the container thus avoid any need for move/copy.

Quimby
  • 17,735
  • 4
  • 35
  • 55
2

Addition to other answers, are you want to use move constructor like

#include<iostream>
#include<vector>

struct Ex
{
    Ex()
    {
        std::cout<<"Default"<<std::endl;
    }

    Ex(const Ex &obj)
    {
        std::cout<<"Copy"<<std::endl;
    }

    Ex(Ex &&obj)
    {
        std::cout<<"Move"<<std::endl;
    }
};

int main()
{
    std::vector<Ex> vec;
    std::cout<<"Pushing"<<std::endl;
    vec.push_back(Ex());
}

Output:

Pushing                                                                     
Default                                                                     
Move    

Make sure that you are using c++11.

Some references:

1)Will std::vectors inside another vector reallocate when the first vector reallocates?

2)STL vector: Moving all elements of a vector

srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
2

Temporary objects gets destroyed at the end of statement, where they are declared;

A(); // will be destroyed at semicolon;

You need to get the object before destroyed, that's when copy constructor takes into place;

A obj = A(); // A object created and copied into obj before gets destroyed.

Same thing happening with vector push_back, temporary created and copied into vector buffer to use it later, before destroyed using copy constructor.

pvc
  • 1,070
  • 1
  • 9
  • 14
1

Its because when you want add something to the vector class and it doesn't have enough space in memory it will need to copy all of its contents to a new space in memory. Therefore the copy constructor gets called, as you are copying the object into a new memory location. Check out this video if you want to know how to optimize this. https://www.youtube.com/watch?v=HcESuwmlHEY

Quick summary of the video If you want to optimize this you can use emplace_back which will allocate the memory for the vector before it will actually add it to the vector. In here you are first creating the object and then adding it to the vector.

Computeshorts
  • 596
  • 1
  • 7
  • 25
  • ` In here you are first creating the object and then adding it to the vector.` Still copy constructor will get called? – Aquarius_Girl Aug 29 '20 at 15:52
  • The copy constructor will also be called when the vector has enough space (when e.g. `reserve` was used). `push_back` will always create a copy of the passed object. In addition to hat copy it might need to do additional moves or copies if the vector has to be resized. – t.niese Aug 29 '20 at 15:54