Am bit confused about the output of constructor calls during erase.
#include <iostream>
#include <vector>
using namespace std;
class sample
{
public:
sample(){ cout<<"Deafult cons\n";}
sample( int a) { cout<<"Param Cons\n"; }
sample( const sample& Obj ) { cout<<"Copy called\n";}
sample( sample &&objj) { cout<<"Copy Reference cons \n";}
sample& operator=(sample&& other){ cout<<"Copy equal cons \n";}
sample& operator=(sample& other){ cout<<"Normal equal cons \n";}
~sample() {cout<<"Destroy\n";}
};
int main()
{
vector < sample > Object;
Object.push_back( sample() );
Object.push_back( sample() );
cout<<" ********* Going to call Erase ********** \n";
Object.erase( Object.begin() );
cout<<" ********* End of call Erase ********** \n";
return 0;
}
The output is
Deafult cons // Understood, creation of temp object
Copy Reference cons // Understood, move assignment
Destroy // Destruction of temp object
Deafult cons // construction of second temp object
Copy Reference cons // Move assignent for second temp to vector
Copy called // Here, i understood coz of memory shortage, vector increased the size and trying to copy the old objects.
Destroy
Destroy
********* Going to call Erase **********
Copy equal cons
Destroy
********* End of call Erase **********
Destroy
My Question,
After size is increased, why system is calling COPY Contructor instead of Move assignment operator or Move constructor to copy/move the old object ?
Coz calling copy constructor again lead to performance issue.
Correct me if am wrong