stl::vector problems (memory?)
Hello, I am experimenting with the stl::vector again, and I have this class:
PHP Code:
struct test
{
int Number;
};
class CTest
{
public:
void Fill();
void Test();
vector<test*> vec;
};
now here is the code:
PHP Code:
void CTest::Fill()
{
//I push one structure.
test t;
t.Number = 5;
vec.push_back(&t);
}
void CTest::Test()
{
char str[20];
sprintf (str, "%i", vec[0]->Number);
MessageBox(0, str, "", 0);
}
Looks as it works? No, it doesnt, the MessageBox shows that Number is garbage and NOT 5 as expected. However, if I test this inside the Fill function (the exact code), it shows the real value assigned to Number!
Oh, I use pointer in the vector, as I might change the Number later, I want it to be updated in the vector.
What is happening here?