While reading the answers to this question I got a doubt regarding the default construction of the objects in the vector. To test it I wrote the following test code:
struct Test
{
int m_n;
Test();
Test(const Test& t);
Test& operator=(const Test& t);
};
Test::Test() : m_n(0)
{
}
Test::Test(const Test& t)
{
m_n = t.m_n;
}
Test& Test::operator =(const Test& t)
{
m_n = t.m_n;
return *this;
}
int main(int argc,char *argv[])
{
std::vector<Test> a(10);
for(int i = 0; i < a.size(); ++i)
{
cout<<a[i].m_n<<"\n";
}
return 0;
}
And sure enough, the Test structs default constructor is called while creating the vector object. But what I am not able to understand is how does the STL initialize the objects I create a vector of basic datatype such as vector of ints since there is default constructor for it? i.e. how does all the ints in the vector have value 0? shouldn't it be garbage?