regarding passing arguements

during function calling.which is better pass by value or pass by const reference
and it is said that in c++ function arguements are sent through const referneces.

but what if the situation is like this
if the object is going to be changed inside the function??
It depends.
If you need to pass primitive type, pass by value.
If you need to pass any type, change value, so it would be visible outside — pass by reference
If you need to pass user defined class in read-only mode — use const reference
If you need to make a copy of passed argument — use pass by value: enable use of move semantic for those who want to use it.
By
which is better
I assume you mean which is faster?

There is an article that discussed speed here:
http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

But that website seems to be down.
So I tried googling the title:
https://www.google.com/search?q=want+speed+pass+by+value

Here are some materials referring to the article that I found (some agree and some disagree):
http://juanchopanzacpp.wordpress.com/2014/05/11/want-speed-dont-always-pass-by-value/
http://stackoverflow.com/questions/21605579/how-true-is-want-speed-pass-by-value

I managed to find a copy of the original article here:
http://joseluisestebanaparicio.blogspot.com/2010/06/want-speed-pass-by-value.html
Last edited on
For simple variables (ints, doubles, etc), passing by value is fine.

If we're talking about objects, then passing by value is inefficient since the object has to be copied onto the stack. Therefore, pass by reference is preferred.

it is said that in c++ function arguements are sent through const referneces.

Said by whom? C++ function arguments are passed however you specify in the function.

if the object is going to be changed inside the function?

Then pass by non-const reference (omit the const keyword).





Topic archived. No new replies allowed.