How to implement this function so that the vector can be passed backto the calling function?

Discussion in 'C++' started by xz, Mar 13, 2008.

  1. xz

    xz Guest

    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.
    I know this happens because of the memory management logic, but I am
    not clear about how to fix this and get what I want.
    Anybody gives some hints?
    Thanks a lot!
     
    xz, Mar 13, 2008
    #1
    1. Advertising

  2. xz

    Kai-Uwe Bux Guest

    Re: How to implement this function so that the vector can be passed back to the calling function?

    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);
    ^^^

    > I know this happens because of the memory management logic, but I am
    > not clear about how to fix this and get what I want.


    It happens because you did "pass by value" not "pass by reference".



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 13, 2008
    #2
    1. Advertising

  3. xz

    James Kanze Guest

    Re: How to implement this function so that the vector can be passedback to the calling function?

    On Mar 13, 7:09 pm, Kai-Uwe Bux <> 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);
    > ^^^


    From 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Mar 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    537
    Joseph Byrns
    Apr 15, 2005
  2. pmatos
    Replies:
    6
    Views:
    24,109
  3. Anand
    Replies:
    2
    Views:
    934
    Anand
    Sep 11, 2003
  4. Replies:
    8
    Views:
    1,996
    Csaba
    Feb 18, 2006
  5. Rob
    Replies:
    3
    Views:
    350
    Daniel Kraft
    Apr 28, 2008
Loading...

Share This Page