I’ve been back & forth with this problem for a while especially since I started to OpenCV library. The fact is, in OpenCV, there are several methods used:
- 1st: funcA((const) CvMat arg)
- 2nd: funcA((const) CvMat& arg)
- 3rd: funcA((const) CvMat* arg)
- 4th: funcA((const) CvMat*& arg) => I’ve just seen and currently been stuck at this
and of course, corresponding to each method, the caller format and the function implementation should be different.
What is the significance about all of these derivatives?? especially the last one (I’ve not yet understood its usage)
Ignoring the
(const)for now, and usingintfor clarity:Pass by value makes a copy in the body of the function
Note that it semantically makes a copy, but the compiler is allowed to elide the copies under certain conditions. Look up copy elision
Pass by reference. I can modify the argument passed by the caller.
Pass pointer by value. I get a copy of the pointer, but it points to the same object pointed at by the caller’s argument
Pass reference to pointer. I can change the pointed itself and the caller will see the change.
As you can see, the second two are just the same as the first two, except that they deal with pointers. If you understand what pointers do, then there is no difference conceptually.
Concerning
const, it specifies whether the argument can be modified or not, or, if the arguments are references or pointers, whether what they point to/refer to can be modified. The positioning ofconstis important here. See const correctness for example.