How to copy STL list in copy constructor

Hi, I would like to copy list elements in copy constructor of my class.
Elements in list are pointers.
Of course, it is important to clone list elements, not only to copy pointers.

Here is my code which doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
MyClass::MyClass(const MyClass& other) : MyBaseClass(other)
{
   _myList.clear();
	
   list<MyBaseClass*>::iterator iter;

   for (iter = other._myList.begin(); iter != other._myList.end(); iter++)
   {
       MyBaseClass* clone = (*iter)->Clone();
       clone->SetParent(*this);
       _myList.push_back(clone);
   }
}


I get ERROR message for iter = other._myList.begin(); and iter != other._myList.end();. it's underlined in code..

Here is first one error message:
56 no match for 'operator=' in 'iter = (((const std::list<MyBaseClass*, std::allocator<MyBaseClass*> >*)(+other)) + 48u)->std::list<_Tp, _Alloc>::begin [with _Tp = MyBaseClass*, _Alloc = std::allocator<MyBaseClass*>]()'

note candidates are: std::_List_iterator<MyBaseClass*>& std::_List_iterator<MyBaseClass*>::operator=(const std::_List_iterator<MyBaseClass*>&)

I'm sure there is better way to do this..
So, please, help :-)
You shall declare


list<MyBaseClass*>::const_iterator iter;

because your copied class declared as const MyClass& other
I didn't remember that..

Thank you..
Topic archived. No new replies allowed.