Reputation: 23
I am working on a programm which has two lists. One of the list stores objects. The other list stores pointers to items of the list containing the objects. When a new item is added to the object list I want to get the pointer to the object. But I always get the same pointer returned.
Since my program is to big now I made a new, smaller one to show you the problem:
#include <iostream>
#include <list>
using namespace std;
class C_Test
{
public:
int a;
};
int main()
{
std::list<class C_Test> Testlist;
C_Test A;
C_Test B;
Testlist.push_back(A);
C_Test * p = &*Testlist.end();
cout << p << endl;
Testlist.push_back(B);
p = &*Testlist.end();
cout << p;
}
Result:
0x22fe20
0x22fe20
I would expect the programm to print out two different pointers since the end of list should have changed? Why does it give me two times the same pointer?
I really would appreciate your help in this matter.
Upvotes: 0
Views: 146
Reputation: 310930
For starters dereferencing the iterator returned by the member function end
has undefined behavior.
I think you mean
C_Test * p = &Testlist.back();
Upvotes: 3