Board index » cppbuilder » Returning TStringList * info required.

Returning TStringList * info required.

Hi Ppl,

Now I'm Not full bottle on C++/BCB, that's why I'm here....
but in BCB3 how do you return an allocated TStringList or use an already
allocated TStringList to add strings to via another procedure?
example
This is what i have :-

Classes::TStringList* __fastcall bigStrList(void)
{
 ... Need code here to add strings to the assignee

Quote
}

void _fastcall TForm1::OtherFunc()
{
  TStringList *myList = new TStringList;
      myList = bigStrList();      // get bigStrList() to return a list
of string into myList

Quote
}

Is there a way to get the strings into myList from bigStrList() without
passing the pointer of myList to bigStrList()?
*and* without creating a new TStringList within bigStrList()? (which
will get lost after bigStrList() exits, or will it?)

Please help!

Thanks for your help,

Matt.

 

Re:Returning TStringList * info required.


Quote

>Classes::TStringList* __fastcall bigStrList(void)
>{
> ... Need code here to add strings to the assignee
>}
>void _fastcall TForm1::OtherFunc()
>{
>  TStringList *myList = new TStringList;
>      myList = bigStrList();      // get bigStrList() to return a list
>of string into myList
>}

>Is there a way to get the strings into myList from bigStrList() without
>passing the pointer of myList to bigStrList()?
>*and* without creating a new TStringList within bigStrList()? (which
>will get lost after bigStrList() exits, or will it?)

You can allocate the memory in bigStrList and as long as you don't free it,
it will be there.  That isn't a very good practice though.  You do typically
want to pass the object into the func instead of having it just return one.
If you intend to go ahead with what you have above, you'll want to change:

Quote
>void _fastcall TForm1::OtherFunc()
>{
>  TStringList *myList = new TStringList;
>      myList = bigStrList();      // get bigStrList() to return a list
>of string into myList
>}

to

Quote
>void _fastcall TForm1::OtherFunc()
>{
>      myList = bigStrList();      // get bigStrList() to return a list
>of string into myList
>}

so that it's just a reference ( or otherwise, you'll be throwing away your
newly created pointer ).

Robert

Other Threads