   Why pass by reference is needed at all?
  Why would you complicate things and instead of this :int some(int A, int B){int C = A + B;return C;}do this :int some(int &A, int &B){int C = A + B;return C;}thanks   
void swap(int & a, int & b){  int temp = a;  a = b;  b = temp;}int x, y;swap(x, y);The above code is identical (and better) than:void swap(int * a, int * b) {  int temp = *a;  *a = *b;  *b = temp;}int x, y;swap(&x, &y); References are almost identical to pointers, but they are syntactically no different than regular members, and references cannot be invalid (it's possible to break that, but with proper use, they're safer than pointers).   ;  You pass by reference when you want to modify the actual-parameter from within the function, when the parameter's data-type is large or when the parameter's instance is non-copyable.In the example of adding two integers you wouldn't pass by reference as it doesn't fulfill any of the above.   ;Quote:Original post by Antheusand references cannot be invalid (it's possible to break thatJust curious, but how?Would this do it?// Try to break the referenceint *pInt = new int; // or assign some random bad address, maybe even NULLdelete pInt;// Would this compile ok and give you a run time error? Or no error, but// possibly undefined behaviour (i.e. overwrite another variable)?swap(*pInt, *pInt);;  Reference semantics are never necessary. However, for performance reasons, it is often useful for a function to modify existing data instead of returning new values. When this is the case, the existing data is passed to the function by reference. As a specific issue to C++, it is sometimes also useful to pass arguments by constant reference in order to avoid unnecessary copies.   ;Quote:Original post by TheMan22Why would you complicate things and instead of this :int some(int A, int B){int C = A + B;return C;}do this :int some(int &A, int &B){int C = A + B;return C;}thanksJust for argument sake(this is no way near optimum) - imagine A and B point to two pieces of geometry with verts, I don't know say the millions, now imagine trying to do processing with your little function when you need to do comparisons between the two objects. Not only are you getting killed by the iterative method, but eventually the memory will become a problem.;Quote:Original post by shurcoolQuote:Original post by Antheusand references cannot be invalid (it's possible to break thatJust curious, but how?Would this do it?More or less. Here are the rules:Null pointer dereferencing:int *p = 0;int &r = *p;Undefined behavior happens when the pointer is dereferenced (and as such, before the reference is created): you are not allowed to dereference a null pointer.. Dead pointer dereferencing:int *p = new int;delete p;int &r = *p;Undefined behavior happens as with null pointers: you are not allowed to do anything with a deleted pointer except overwriting its value.Deleting the referenced object:int *p = new int;int &r = *p;delete p;Undefined behavior happens when the pointer is deleted, as you may not delete a value bound to a reference.Referenced object going out of scope:int &f() {   int i = 10;  return i;}Undefined behavior happens when the function returns, after the reference is created. You may not reference an auto value once it goes out of scope.However, the following is valid: int f() { return 10; }int main() {   const int &i = f();}The temporary value bound to the reference has its lifetime extended until the reference variable goes out of scope.;Quote:Original post by shurcoolQuote:Original post by Antheusand references cannot be invalid (it's possible to break thatJust curious, but how?Would this do it?// Try to break the referenceint *pInt = new int; // or assign some random bad address, maybe even NULLdelete pInt;// Would this compile ok and give you a run time error? Or no error, but// possibly undefined behaviour (i.e. overwrite another variable)?swap(*pInt, *pInt);To quote myself:Quote:(it's possible to break that, but with proper use, they're safer than pointers).Why are you dereferencing pointers? Why are you allocating an object in the first place?Proper use of references severely reduces the amount of pointers, since they perform exactly the same function.Even more, depending on the compiler, the above may trigger an exception, wheras with pointers, the application will or won't crash.The above example, when using references, would look like:int i;swap(i, i);But yes, C++ is god-like language. Name anything, it can be broken. This is why sane approach, not C-like fallbacks. Pointers in C++ should be really used for things that are heap allocated. And even then, several of such situations can be avoided.....Off-topic:template < class T >void swap( T & a, T & b ){  T temp(a); a = b; b = temp;}template < class T >void swap( T * a, T * b ){  assert( a!=NULL && b!= NULL); // as good as it gets  int temp(*a); *a = *b; *b = temp;}swap( x, y ); // don't care what x and y areA technical reason against pointers: Using pointers for swap needs to take into consideration aliasing ( swap(x, x); ). As such, compiler cannot optimize obvious self-assignment (aliasing of same memory).   ;  Cool, thanks for the details ToohrVyk.Btw, I didn't know you could create reference types within regular code; I thought it was only avaliable for function parameters. That's pretty neat.   ;  Also I'm pretty sure that in C++ if you pass structs/classes and don't do it by reference (or pointers) then it copies the structs/classes into temporary ones, which will result in a performance hit, I think.  Someone with more knowledge correct me if I'm wrong.   