Hi, mates.
I wrote the function and it runs properly, but i don't know how to make it return a List object correctly.
It should be like: List list = Function(arguments);
but when i type it thatway it crushes.
Should my function be of a List& type ?
Thank you in advance!
I guess I'm too stupid but it doesn't work for me...
My code is like that :
1 2 3 4 5 6 7 8
List Function (arguments)
{
...
return list;
}
List* list = &Function(arguments);
list->print()
Everything else works fine... even if i move the body of the function to the main() it works as i want it to work... but that problem with returning a list I cant fix :(
IIRC, this tries to find the data address of the function...make the function return a List* then just set the List* you are creating to the result of the function.
Why does the signature say the function returns a List instead of a List *?
You're trying to return a pointer to a local object. Local objects are destructed when the function that created them returns, so that means you're returning an invalid pointer.
Read this on dynamic memory allocation: http://www.cplusplus.com/doc/tutorial/dynamic.html
Ok, but how can i use a pointer to a list, which is destructed then ? :(
Then i should return the list itself ??
i wanted it to be something like that :
1 2 3 4 5 6 7 8 9 10 11
List* Function (List& list1, List& list2)
{
List list = list1;
...
List* asdf = &list
return asdf;
}
List* list = Function(arguments);
list->print()
}
I understood that after returning the pointer asdf, list is destructed... Can i saomehow preserve destructing it while executing the whole program or is there any other appropriate solution ?