returning vector reference

Discussion in 'C++' started by tornado, Jul 18, 2003.

  1. tornado

    tornado Guest

    hi all,

    i am pretty new to c++. i have this problem for which i am unable to
    think a solution. i don't understand how to pass a vector refernce
    back to the callin function. And how this reference will be handled by
    the calling function ? Can any one in the group point me to the
    correct solution for it ? Any code snippets will be of great help.

    Thanks in advance.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    tornado, Jul 18, 2003
    #1
    1. Advertising

  2. I'm not subscribed to .moderated group, so I'm replying in c.l.c++ only
    "tornado" <> wrote...
    > i am pretty new to c++. i have this problem for which i am unable to
    > think a solution. i don't understand how to pass a vector refernce
    > back to the callin function. And how this reference will be handled by
    > the calling function ? Can any one in the group point me to the
    > correct solution for it ? Any code snippets will be of great help.


    The usual way is to either ask the user of the function to pass the
    vector in by reference (and then you can return the same reference)
    like here:

    vector<int>& function(vector<int>& v)
    {
    .. // do something with 'v'
    return v;
    }

    void callingfunction()
    {
    vector<int> v;
    function(v).size(); // it passes 'v' and uses the
    // return value to call 'size()'
    }

    or, instead of returning a reference, return an object:

    vector<int> function()
    {
    vector<int> v;
    // do something to v
    return v;
    }

    void callingfunction()
    {
    vector<int> v = function(); // no references here
    }

    The compiler will take care of optimising the copying.

    Victor
    Victor Bazarov, Jul 18, 2003
    #2
    1. Advertising

  3. tornado

    Andy Sawyer Guest

    In article <>,
    on 18 Jul 2003 14:52:47 -0400,
    (tornado) wrote:

    > hi all,
    >
    > i am pretty new to c++. i have this problem for which i am unable to
    > think a solution. i don't understand how to pass a vector refernce
    > back to the callin function. And how this reference will be handled by
    > the calling function ? Can any one in the group point me to the
    > correct solution for it ? Any code snippets will be of great help.


    A little more information on what you're trying to do
    (syntactically-valid, compileable code snippers for instance) would help
    :)

    My guess (and that's all it is) is that you're probably trying to
    return a reference to a locally created vector. Something like:

    std::vector<int>& fubar()
    {
    std::vector<int> v;
    // do stuff
    return v;
    }

    In short - don't do this. It's always a bad idea, and invokes undefined
    behaviour (since the vector your reference refers to will be destroyed
    at the end of the function, so your reference will no longer refer to it).
    Undefined behaviour is something you should avoid wherever possible,
    even if you "know" that the current compiler/library implementation you
    are using will "do what you want" - the next release may not do.

    You can do something like:

    std::vector<int>& foo( std::vector<int>& v )
    {
    // do stuff...
    return v;
    }

    And construct the vector before you call the function. Again, depending
    on what you're trying to achieve, it might be a more elegant solution to
    pass an output iterator of some sort to your function, thus:

    template<typename OutputIterator>
    OutputIterator bar( OutputIterator out )
    {
    // do stuff - "store" your results by writing
    *out++ = some_value;
    // ...
    return out;
    }

    And you caller looks something like:

    std::vector<int> v;
    bar( std::back_inserter( v ) );

    Of course, your called might one day want to use (e.g.) a deque as the
    container, so the caller can change to:

    std::deque<int> v;
    bar( std::back_inserter( v ) );

    and leave your function unchanged.

    Regards,
    Andy S
    --
    "Light thinks it travels faster than anything but it is wrong. No matter
    how fast light travels it finds the darkness has always got there first,
    and is waiting for it." -- Terry Pratchett, Reaper Man

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Andy Sawyer, Jul 20, 2003
    #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. pmatos
    Replies:
    6
    Views:
    23,721
  2. Replies:
    8
    Views:
    1,887
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    541
    James Kanze
    Sep 4, 2007
  4. JustMe
    Replies:
    1
    Views:
    163
    Tassilo v. Parseval
    Aug 29, 2003
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    343
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page