22.6 Pass by reference

The & symbol is a way of telling the compiler to pass an object by reference, that is, the compiler generates a pointer to the object and passes that, instead of copying out all the fields of the object. If you have a function with a prototype like the following, you can give it a cSomeClass argument input and Func can in fact change what's in the input.

 int Func(cSomeClass &input); 

One reason we do this is because we might actually want to change the fields of the object being passed. Recall that in C, you can't directly change the value of a variable being passed into a function. But the C++ trick of putting an & into the function prototype lets you have a function which does change the values of its input.

Another reason why we sometimes want to pass an object as a pointer instead of as a structure is that it's faster to pass a pointer to an object instead of copying the whole object. And there will be times you want this speed, but you definitely don't want to change what's in the input.

The troublesome const also arises in connection with the & symbol in function declarations. Here it relates to the argument of the function rather than to the caller. If you want the speed of passing an argument by reference, but you know you don't want to actually change what's in the object, then you use the const followed by the class name and the & to mean 'don't allow any change in this fields of this object, but do pass it as a pointer which you generate.' And you use a prototype like:

 Func(const cSomeClass &input); 

You can combine the const argument and the const function declarations in all the possible ways. C++ views all of these as different functions.

 int Func(SomeClass &input)const;  int Func(SomeClass &input);  int Func(const SomeClass &input)const;  int Func(const SomeClass &input); 

For full disclosure, we might as well mention four more possible ways that you might prototype a function in C++.

 int Func(SomeClass input)const;  int Func(SomeClass input);  int Func(SomeClass *input)const;  int Func(SomeClass *input); 

What a hassle, huh? A real nightmare! No wonder so many people want to abandon C++ for the calm of Java or C#. Bjarne Stroustrup, the inventor of C++, claims that the only reason that Java is simple is because it's a young language lacking all of C++'s features. He says that C++ is so complicated because it's mature.

But once you get some practice with using C++, you'll find that having eight possible ways to prototype a function isn't so bad as you might expect. The practice is always to make every function as const as you can, so you don't actually have to think about that so much. The different forms let you make sure your code is going to be as portable, safe, and fast as possible.

Remember to match the *.cpp implementation format to the *.h declaration format. And, above all, be careful that your derived classes use the same declarations as the parent classes.

Software Engineering and Computer Games
Software Engineering and Computer Games
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net