   C++ Reference Parameters
  Say I have a function like this:void setPosition(const Vector3& pos);What happens if I pass in an argument like this?setPosition(Vector3(0.0, 0.0, 0.0));I'm interested in knowing about the lifetime of the passed in reference, is this valid?  From what I can guess, a new Vector3 object is created and exists for the entire lifetime of this function, and in this case it would be equivalent to the following:void setPosition(Vector3 pos);setPosition(Vector3(0.0, 0.0, 0.0));Thanks.   
  1) the first one, with the reference only works for inline temporaries because you have the word "const" there. Take that out, and you can't call it the way you did.2) Your first example creates a temporary that will die after the function returns.3) Your second example creates a temporary that will die after the function returns. It then makes a copy of that object for the function to use.  With optimizations off, that means extra work. With optimizations on, the compiler can probably remove the extra copy, but it might not. Optimizations might even just inline the setPosition call. All up to the compiler.;  Almost equivalent. You would be unable to modify pos inside of setPosition in the first example, but you could in the second example. Other than that, they're functionally the same.   ;  The reference will bind to the temporary and the lifetime will be extended to the lifetime of the function.  In other words, you can safely read the value, or copy it, but don't store a pointer to the Vector3 and try to access it later.  I might suggest you read this:http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/   ;Quote:Original post by AristeAlmost equivalent. You would be unable to modify pos inside of setPosition in the first example, but you could in the second example. Other than that, they're functionally the same.If you're going to say that, then you should probably also clarify that in neither case would such modifications be visible externally. I.e. they do not affect the caller's arguments.   ;Quote:Original post by iMalcQuote:Original post by AristeAlmost equivalent. You would be unable to modify pos inside of setPosition in the first example, but you could in the second example. Other than that, they're functionally the same.If you're going to say that, then you should probably also clarify that in neither case would such modifications be visible externally. I.e. they do not affect the caller's arguments.Well, in the first case, no such modifications are possible. But yes, in the second case, modifications affect only a local copy of the passed-in parameter, and will therefore be invisible outside of the function.   ;  As far as I understand it (and hopefully it's true, my C++-fu isn't that keen), a reference parameter is exactly the same as passing a pointer to a function with two differences:* You can't do pointer arithmetic or array indexing* Every time you use the variable in an expression, it is automatically dereferenced.So the two snippets are equivalent:void Medkit::heal(Player* p){  p->setHealth(p->getHealth() + 50);}void Medkit::heal(Player& p){  p.setHealth(p.getHealth() + 50);}If you passed Player p without making it a reference, it would invoke the copy constructor and none of the changes would appear outside the function (sort of like What Happens in Vegas Stays in Vegas).Passing by reference is very similar to how variables are passed by default in languages like Java, C#, Python, and many others. In fact, it's exactly the same for objects. For primitives, C++ behaves a little differently, because primitives are immutable in all those languages.   ;  There are additional differences between reference and pointer arguments, some of which rarely matter in practice (you can't get the address of the reference variable itself), but one that does matter is that const reference parameters can be bound to rvalues such as temporaries. Otherwise, with pointers and non-const reference arguments you can't use a temporary (barring severely evil code or running MSVC with extensions enabled which will allow you to bind an rvalue to a non-const reference).Also, Java and C# don't have immutable primitives. What you probably wanted to say is that they use pass-by-value for primitives. Though C# will let you declare ref and out parameters that will allow modifying function arguments. Ex:class RefExample{    static void Method(ref int i)    {   i = 44;    }    static void Main()    {   int val = 0;   Method(ref val);   // val is now 44    }};Quote:Original post by SiCraneAlso, Java and C# don't have immutable primitives. What you probably wanted to say is that they use pass-by-value for primitives. True. I was making a blanket statement. But they are effectively immutable because they are always passed by value.Python, Ruby, and any sufficiently object-oriented or functional language will use pass-by-reference for everything, but keep "primitives" (integers, strings, booleans, etc.) immutable.   ;Quote:Original post by Tac-TicsBut they are effectively immutable because they are always passed by value.1) Your definition of "effectively immutable" is bizarre. Just because something can't be modified by passing it as a function argument doesn't mean that it can't be changed in other ways - that are common, idiomatic uses of the language. 2) I just showed you how C# can use pass by reference for primitives. Therefore, even with that definition, C# doesn't have immutable primitives.   