The reason I have to do this is
" AList* aList = new AList(10); "
I have to keep the list around (even when the function returns)
something like this:
AList& myFunc() {
AList* aList = new AList(10);
// do somethin here with aList;
return (*aList);
}
That looks like a recipe for memory leaks. When a function returns a
reference, the caller doesn't normally expect to be saddled with the
responsibility for deleting the thing referenced.
Incidentally, what do you do with the returned reference? If you use it
to initialise another reference in the caller, you'll have a problem
making it exception-safe; if you copy it to somewhere else you might as
well have returned the AList by value in the first place.
The reason I do 'typedef A* APtr;' is I can switch APtr to be a smart
pointer of A instead of a c++ raw pointer of A in the future.
But as you've written the code above, the pointer is only used within
the function. If the function is transferring ownership to the caller,
it would be much clearer to have it returning a (smart) pointer.
But don't do this as a hack. If you don't know who is supposed to own
what, you'll end up with memory leaks or, worse, heap corruption caused
by double deletions. Start by defining an ownership policy and then
write code to enforce it.