By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Does containers get copied when used as parameters?

P: n/a
When I do something like:

void Foo(string s)
{
// something here
}

does ti copy S, or as with a char* it only copies the location where
the string is?

More generally when I use a container as parameter does it pays to pass
a reference as a parameter to avoid the container being copies?

Thanks!

Jul 2 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
When I do something like:
>
void Foo(string s)
{
// something here
}

does ti copy S, or as with a char* it only copies the location where
the string is?
It copies s.
More generally when I use a container as parameter does it pays to pass
a reference as a parameter to avoid the container being copies?
Yes.

void Foo(string& s)
{
// something here
}

//eric
Jul 2 '06 #2

P: n/a
>More generally when I use a container as parameter does it pays to pass
>a reference as a parameter to avoid the container being copies?

Yes.
I forgot to say: that is if you're not going to change the value of the
parameter. However, in some cases where you change the value of the
parameter and the caller no longer will use the variable it still pays. But
i recommend that you will only pass a variable as a reference if you're not
changing it.

void foo (const std::string& s) {
// whatever
}

//eric
Jul 2 '06 #3

P: n/a

"Eric Jensen" <er**@no.spam.comwrote in message
news:44***********************@dread11.news.tele.d k...
>>More generally when I use a container as parameter does it pays to pass
a reference as a parameter to avoid the container being copies?

Yes.

I forgot to say: that is if you're not going to change the value of the
parameter. However, in some cases where you change the value of the
parameter and the caller no longer will use the variable it still pays.
But i recommend that you will only pass a variable as a reference if
you're not changing it.

void foo (const std::string& s) {
// whatever
}
Are you only referring to a const reference here? Passing by non-const
reference is a perfectly fine way to pass objects whose contents may be
modified.

-Howard
Jul 3 '06 #4

P: n/a
Are you only referring to a const reference here? Passing by non-const
reference is a perfectly fine way to pass objects whose contents may be
modified.
I nearly meant, if the argument are not to be changed, always pass as a
reference.
And if the caller expect that the argument is not to be manipulated, then do
not pass as a reference if the function modifies the object. And of course
you can still pass the argument as a reference, if the purpose of your
function is to modify the object for the caller.

//eric
Jul 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.