When you return an array, you actually return a pointer to an array. On the whole you need to allocate storage for that array as well as populating it and the calling code needs to know if it needs to free up the memory allocated to the array or not, which means documentation. Yuck.
"Arrays are evil." - see the C++ FAQ.
The C/C++ array is even more primitive than Java's and requires you to manage memory.
But... if you return a vector, memory management is done for you. The C++ vector is analogous to java.util.Vector.
Main Topics
Browse All Topics





by: Andrewm1986Posted on 2007-02-12 at 05:30:55ID: 18514623
"Can an object method return an array or will it only work if the array is created outside the method and passed as an argument? "
Yes. Arrays created inside the method body will be out of scope when they reach the end, and so possibly over written.
You said yourself that it is common practice to pass a pointer into the method, this is the way it should be done.
Pointers and references are tottaly different, I would look up these differences.
Andrew