Use references when a parameter is to be passed by reference; the called function forgets about the argument as soon as it returns. Use a regular reference if you are going to modify the argument (TFoo&
), and a const
reference if you aren't going to modify it but don't want the overhead of call by value (const TFoo&
).
Use pointers when the function you call retains a reference (an alias) to the object you are passing in, such as when you construct a dynamic data structure. For example, when you put an object into one of the Taligent Collection classes, the collection retains a pointer to your object. Explicitly using pointers lets the reader know that aliasing is occurring.
Developers sometimes pass in a nil pointer to indicate a default value. The correct way to achieve the same effect is to provide a reference with a static default argument, or to overload the function:
class TFoo {
public:
static const TBar kDefault;
Technique1(const TBar &arg = kDefault);
Technique2(const TBar &arg);
Technique2();
};
[Contents]
[Previous]
[Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.