I just realized that I can access object members of my empty object vector list. I thought vector.reserve(nmb) just reserves the required memory (kind of nmb*sizeof(object)).
#include <iostream>
#include <vector>
class Car {
public: int tires = 4;
Car(void) {
std::cout << "Constructor of Car" << std::endl;
}
};
int main()
{
std::vector<Car> carList;
carList.reserve(20);
std::cout << "Car tires: " << carList[0].tires << std::endl;
std::cout << "Now comes the emplace_back:" << std::endl;
carList.emplace_back();
std::cout << "Car tires: " << carList[0].tires << std::endl;
//Car carArray[20];
//std::cout << "Car tires: " << carArray[0].tires << std::endl;
return 0;
}
gives me:
Car tires: 0
Now comes the emplace_back:
Constructor of Car
Car tires: 4
...Program finished with exit code 0
Why can I access members of not yet initialized objects? Thank you.