A.Pope A.Pope - 2 years ago 95
C++ Question

Why does list.end() not change when new item is added?

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
int a;

int main()
std::list<class C_Test> Testlist;
C_Test A;
C_Test B;
C_Test * p = &*Testlist.end();
cout << p << endl;
p = &*Testlist.end();
cout << p;



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.

Answer Source

For starters dereferencing the iterator returned by the member function end has undefined behavior.

I think you mean

 C_Test * p = &Testlist.back();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download