Why do you pass objects by reference?

I was watching one of bucky's tutorials about friend functions and this was the code he used in his example:

#include <iostream>
using namespace std;

class StankFist
{
        public:
                StankFist(){stinkyVar=0;}
        private:
                int stinkyVar;
        friend void stinkysFriend(StankFist &sfo); //My question is about this
};
void stinkysFriend(StankFist &sfo)
{
        sfo.stinkyVar=99;
        cout << sfo.stinkyVar << endl;
}

int main ()
{
        StankFist bob;
        stinkysFriend(bob);
}


I don't remember being taught the reason why you have to pass by reference can somebody point me to a place to read more about this or explain to me why it is done like this?
Last edited on
Main reasons:
* Modify the parameter.
* Prevent unnecessary (and possibly expensive) copies.

In your example, it's the former.
Thanks!
Topic archived. No new replies allowed.