473,513 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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++.moderated. First time posters: Do this! ]
Jul 19 '05 #1
2 29508
I'm not subscribed to .moderated group, so I'm replying in c.l.c++ only
"tornado" <to********@yahoo.com> 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
Jul 19 '05 #2
In article <c8**************************@posting.google.com >,
on 18 Jul 2003 14:52:47 -0400,
to********@yahoo.com (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! ]
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
11894
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
4
1760
by: Patrick | last post by:
I want to achieve the following behaviour but I am having trouble getting there...bare with me im knew to c++ , so its probably rather trivial! To have a class ClassA, and composed within this...
18
2116
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
8
2079
by: Derek | last post by:
Some authors advocate returning const objects: const Point operator+(const Point&, const Point&); ^^^^^ Returning a const object prevents some bad code from compiling: Point a, b, c; (a +...
8
1736
by: Richard | last post by:
what is the syntax for returning a vector? temp is a vector return temp; ? return temp<>; ? return temp<int>; ?
13
2291
by: Gernot Frisch | last post by:
Which method is the fastest/best: std::vector<intfoo1() { std::vector<intv; ... reutun v; } std::vector<int>& foo2()
3
8791
by: Michele | last post by:
Hello: What is the syntax for returning a reference to a vector from a function? I have a private vector and I want to return it using a public get function (like in the code below), but I...
7
2392
by: arnuld | last post by:
/* C++ Primer - 4/e * * 1st example from section 7.2.2, page 234 * returning 2 values from a function * * STATEMENT: * to find a specific value in a vector and number of times * that...
23
2902
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.