Well.... that isn't really an example of passing objects by reference at all... the only place a reference is used is in the copy constructor but that never gets called in this example![]()
Okay well an alternative version of FunctionTwo() using references would be this:
Not much different as you can see, but it's a little bit simpler than the pointers example. Basically you just treat the reference as if it was the original object, it works just the same.Code:const SimpleCat & FunctionTwo (const SimpleCat &theCat) { cout << "Function Two. Returning...\n"; cout << "Frisky is now " << theCat.GetAge(); cout << " years old \n"; // theCat.SetAge(8); const! return theCat; }
The main difference is really how you would use it. You don't pass the address, you pass the actual object. Like this:
It can make things a little easier.Code:FunctionTwo(Frisky);




Reply With Quote