Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 702
  • Last Modified:

CList class object as return type

Hi,
  I need to have a CList object as return type. My code goes like this.

class EERecordVector
{
public:
      char Action;
      EERecord Record;
};

typedef CList<EERecordVector*,EERecordVector*> EERecordList;

suppose in a method like

EERecordList    CTest :: testMethod( )
{
    EERecordList   reclist;    
    EERecordVector *RecordVector =new EERecordVector();
    RecordVector->Action = 'a';
    RecordVector->Record = "test record";
    reclist.AddTail(RecordVector);   //keep on adding values like this
   return (reclist) //need to return the CList
}

I call the method as
CTest test;
EERecordList   recs = test.testMethod();

this throws error.....

can anyone suggest.......
0
abi_a
Asked:
abi_a
  • 3
1 Solution
 
SteHCommented:
Can it be CList& or CList* as well?
0
 
SteHCommented:
Your prog has the problem that the list will be out of scope, when testMethod is left. reclist is only created on the stack. In the sample above you need to change this to:

EERecordList*    CTest :: testMethod( )
{
    EERecordList*   preclist;    
    EERecordVector *RecordVector =new EERecordVector();
    RecordVector->Action = 'a';
    RecordVector->Record = "test record";
    preclist->AddTail(RecordVector);   //keep on adding values like this
   return (preclist) //need to return the CList
}

don't forget to delete the returned pointer when it is not needed anymore.
0
 
SteHCommented:
Sorry, first line in function should read:

    EERecordList*   preclist = new EERecordList;
0
 
abi_aAuthor Commented:
thanks ...it works
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now