Original Post
I encountered a really wierd thing, when im trying push struct to vector and return pointer to the struct in the vector:
But like you see from the comments that doesnt work, but why?
#include <iostream>
#include <vector>
using namespace std;
struct DING
{
int k;
};
vector <DING> kkk;
DING* getsom()
{
DING k; // temp
k.k = 5;
kkk.push_back(k);
return &kkk.back(); // trying to return last element
}
int main()
{
DING* t1 = getsom();
DING* t2 = getsom(); // this will somehow ''overwrite'' t1''s address
// t1 is invalid pointer!
cout<<(t1->k)<<" "<<(t2->k)<<endl;
return 1;
}