I find myself needing to keep two lists of objects for performance reasons (speed over memory).
I’m using object pointers such as
std::list<MyClass*> m_list1;
std::list<MyClass*> m_list2;
I add objects to the list by
MyClass* myObject = new MyClass();
m_list1.push_back(myObject);
m_list2.push_back(myObject);
The problem I’m facing is that changes I make to m_list1 are not affecting m_list2, so clearly they are copies and not the real deal.
What is the correct way to store these so they won’t be copies? Not using pointers isn’t an option as these will all be allocated on the heap.
Std containers copy the values passed in, but since you passed in pointers, it is the pointers that are copied. Therefore they are still pointing to the same object. The problem must be somewhere else. Perhaps you could post the specific code that you think is exhibiting this behavior?