Consider the below program:
class A
{
public:
A(int i)
{
cout<<"Called"<<endl;
}
};
int main()
{
vector<A> v(5,A(1));
return 0;
}
I am getting the output: http://ideone.com/81XO6
Called
Why the constructor gets called only once even if we are constructing 5 objects?
How vector
is internally handled by the compiler?
A(1)
. That temporary is copied into the vector five times. It cannot be constructed directly in the vector as you have it. (Well, I guess it could, if your compiler was smart enough. But it's not.) @Pooya: What are you talking about? That is definitely not the case.