When reading the answers to this question , I had a doubt about the default construction of objects by default. To test this, 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, of course, the default constructor of the Test constructor is called when the vector object is created. But I canβt understand how STL initializes objects, am I creating a vector of a basic data type, such as an ints vector, since there is a default constructor for it? those. how do all ints in a vector have a value of 0? Isn't that trash?
c ++ stl default-constructor built-in-types
Naveen
source share