Link to home
Start Free TrialLog in
Avatar of trongtran
trongtran

asked on

CObList operator=


Hi All,

I'm trying to create a link list using MFC CObList. The list is loaded
ok, but now I need to create another list (nested) inside that list.
After making the list I have to assign it to a member variable (of
type CObList) in the parent list.
How can I write the operator= for CObList?
or Is there a better way.
Here's a sample (this is just a pseudo code):

class CNestedData : public CObject {...} //the nested node
class CNestedList : public CObList  {
      CNestedList &operator=(const CNestedList & nlist);
}
class CData : CObject {
      //my other data of vary type (int, double, CString...)
      CNestedList *nestedList;
}

and create the list like this:
Nested List:
      CNestedData nestedData(...); //inserting data
      CObList nList;
      nList.AddHead(nestedData);
Parent List:
      //inserting data and copy the
      //nested list to nestedList
      CData thedata(..., nList);
      CObList thelist;
      thelist.AddHead(thedata);

How do I insert the nested list into the CData list
("..." means other data I have to insert to the list).
I tried to write the operator= like this:
(This function works but the nested lists are added up)
CNestedList& CNestedList::operator=(const CNestedList& nlist)
{
      CNestedData *theCopy;

      if(clist.GetCount()){
            POSITION pos = clist.GetHeadPosition();
            while(pos){
                  theCopy = (CCopy *)clist.GetAt(pos);
                  AddHead(theCopy);
                  clist.GetNext(pos);
            }
      }
    return *this;
}

I'm fairly new to MFC. Thanks for any help.

TT.
ASKER CERTIFIED SOLUTION
Avatar of trestan
trestan
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trongtran
trongtran

ASKER

You're right, I'm sorry. I did used the derived CNestedList instead of CObList. I tried to pass argument by reference but I still need to write the operator=, don't I? (class CObList doesn't have one).
Anyway, I realized that I can use AddHead(CObList *) function instead. Still it isn't loading the list right.
Is there a better way to create the nested list?.