The main problem with your conventions is that you make no allowance for the possibility of interfacing to code (e.g. written by someone else) that doesn't follow your conventions.
Generally speaking, I use a different set of conventions, and rarely find a need to work around them. (The main exception will be if there is a need to use a pointer to a pointer, but I rarely need to do that directly).
Passing by non-const
reference is appropriate if ANY of the following MAY be true;
- The object may be changed;
- The object may be passed to another function by a non-
const
reference [relevant when using third party code by developers who choose to omit the const
- which is actually something a lot of beginners or lazy developers do];
- The object may be passed to another function by a non-
const
pointer [relevant when using third party code be developers who choose to omit the const
, or when using legacy APIs];
- Non-
const
member functions of the object are called (regardless of whether they change the object or not) [also often a consideration when using third-party code by developers who prefer to avoid using const
].
Conversely, const
references may be passed if ALL of the following are true;
- No non-
mutable
members of the object are changed;
- The object is only passed to other functions by
const
reference, by const
pointer, or by value;
- Only
const
member functions of the object are called (even if those members are able to change mutable
members.
I'll pass by value instead of by const
reference in cases where the function would copy the object anyway. (e.g. I won't pass by const
reference, and then construct a copy of the passed object within the function).
Passing non-const
pointers is relevant if it is appropriate to pass a non-const
reference but there is also a possibility of passing no object (e.g. a nullptr
).
Passing const
pointers is relevant if it is appropriate to pass a const
reference but there is also a possibility of passing no object (e.g. a nullptr
).
I would not change the convention for either of the following
- Storing a reference or pointer to the object within the function for later use - it is possible to convert a pointer to a reference or vice versa. And either one can be stored (a pointer can be assigned, a reference can be used to construct an object);
- Distinguishing between dynamically allocated and other objects - since I mostly either avoid using dynamic memory allocation at all (e.g. use standard containers, and pass them around by reference or simply pass iterators from them around) or - if I must use a
new
expression directly - store the pointer in another object that becomes responsible for deallocation (e.g. a std::smart_pointer
) and then pass the containing object around.
*obj = get()
or&obj = *get()
?Object &obj = *getOrCreateObject();
won't be a good choice because function can legally return a null pointer and you'll end up with invalid reference. If the function is not supposed to return a null pointer value then there is no point in returning pointer instead of a reference.Object &obj = getOrCreateObject();
if I want to work on that specific object, not a copy?