Re: How to implement this function so that the vector can be passed
back to the calling function?
On Mar 13, 7:09 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
xz wrote:
I wrote a function which looks like:
bool Xxx::findShortestPath(int xS, int yS, int xT, int yT,
vector<Vertex*> path) {
...
}
It meant to find a path, save the path in vector<Vertex*> path, and
pass the path back to the calling function.
And in some other function (say, main()), I called this function like:
int main() {
...
vector<Vertex*> path;
xxx.findShortestPath(0, 0, 9, 9, path);
// then print out the path
}
However, I just noticed that it failed to passed the path to the
main() function. That is, in the function findShortestPath(...), the
data has been loaded into *path*, but when it goes back to main(), all
data is lost.
Make the signature:
bool Xxx::findShortestPath(int xS, int yS, int xT, int yT,
vector<Vertex*> & path);
^^^
=46rom a purely conceptual point of view, something like:
Fallible< std::vector< Vertex* > >
Xxx::findShortestPath( ... ) ;
would be better. In practice, if you call the function a lot,
and the paths tend to be fairly large, the performance
implications may become an issue.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34