returning vector reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tornado

    returning vector reference

    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++.m oderated. First time posters: Do this! ]
  • Victor Bazarov

    #2
    Re: returning vector reference

    I'm not subscribed to .moderated group, so I'm replying in c.l.c++ only
    "tornado" <tornado579@yah oo.com> wrote...[color=blue]
    > 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.[/color]

    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).siz e(); // 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


    Comment

    • Andy Sawyer

      #3
      Re: returning vector reference

      In article <c8b0a275.03071 70446.60b957ac@ posting.google. com>,
      on 18 Jul 2003 14:52:47 -0400,
      tornado579@yaho o.com (tornado) wrote:
      [color=blue]
      > 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.[/color]

      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<typena me 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_inser ter( 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_inser ter( 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++.m oderated. First time posters: Do this! ]

      Comment

      Working...