Practices - Argument Passing
The following rules apply to argument passing.
-
When passing an argument of a non POD type that will not change in the function, use const references ( const & )
-
When passing an argument that may change use normal reference
-
Objects of classes with difficult to follow lifecycle or complicated / costly copy semantics shall be passed by boost::intrusive_ptr
If none of these rules apply, or additional constraints make them not feasible, then follow the paradigm:
-
Safety of operation first
-
Clarity of intent second
-
Speed last
If a need arises to use raw pointers as arguments then the following rules apply:
const &, const * - ownership stays in the calling code
&, * - ownership stays in the calling code, value may be freely changed
** - ownership is transferred to callee, calling code has to explicitly zero the variables which held the code or overwrite the variable
some_class function1( arg const & _first , arg2 * _second , arg3 ** _third );
It is assumed that _first is immutable, _second may be changed by the function1 and management of _third is up to the function1.
These rules may be changed when dealing with an interface from an external library