The first print shows the member value to be false, and the other two prints show it as true. Why does the first output differ from the last two?
#include <vector>
#include <iostream>
using namespace std;
class MyClass
{
public:
bool value;
bool stuff;
};
class Container
{
public:
vector<MyClass> my_classes;
Container()
{
MyClass c;
cout << c.value << endl;
my_classes.push_back(c);
}
};
int main (int argc , char* argv[] )
{
MyClass mc;
cout << mc.value << endl;
Container con;
cout << con.my_classes[0].value << endl;
return 0;
}
The two members are primitive (non-class) objects and thus uninitialized. That means that their values will be arbitrary at runtime. You must initialize them in the constructor: