Can someone please give some examples where pass by reference is preferred over pass by pointer.
Basically both offer the same functionality but I want to know where pass by reference becomes mandatory.
Also, please suggest how to decide to use either of the two.
Please suggest with some basic examples.
Copy constructor and Assignment operator.
These are mandatory situations. Apart from these there can be other situations also (e.g. overloading other operators like
+,<,>and so on).I prefer references (more C++ style) at most of the places and if a reference can’t be used, I tend to use pointers (more C style).
Reference are somewhat analogical representation of a
constpointer; i.e.A&is similar asA* const. Thus they provide some in-built safety against overwriting the location accidently.