Solved

CObList operator=

Posted on 1998-07-27
2
458 Views
Last Modified: 2013-11-20

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.
0
Comment
Question by:trongtran
2 Comments
 
LVL 8

Accepted Solution

by:
trestan earned 100 total points
Comment Utility
Try to use pointer or reference to pass the address of the object into the class using it. E.g.:
In your CData class, declare a constructor like this: CData(..., CNestedList * TempList){nestedList=TempList}; Create the instance like this: CData thedata(..., &nList); Pls try to see if it works. BTW, you derived CNestedList from CObList, but you still use CObList in your codes. Did you mean to use it or just derived to overload "="?  
0
 

Author Comment

by:trongtran
Comment Utility
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?.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 51
ODBC Connection Logging, ADO.NET 6 49
pre4 challenge 19 87
sumHeights2  challenge 7 75
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now