I am trying to create a vector of a class with parametrized constructor.
#include <iostream>
#include <vector>
using namespace std;
struct foo
{
foo() {
cout << "default foo constructor " << endl;
}
foo(int i)
{
cout << "parameterized foo constructor" << endl;
}
~foo() {
cout << "~foo destructor" << endl;
}
};
int main()
{
std::vector<foo> v(3,1);
}
I was expecting that there would be 3 calls to the parameterized foo constructor
but instead I get the output as
parameterized foo constructor
~foo destructor
~foo destructor
~foo destructor
~foo destructor
What is happening here ?
How can I use constructor of vector such that objects of class get created with parametrized constructor?