COW - Copy On Write

Before talking about cow, the copy-on-write, lets have a look at calling a method.

{
  foo ( x, y );
  foo ( &x, &y );
  foo ( x, y );
  foo ( x, y );
}
.................................
foo ( QString a, QString b ) { }                                                 // I
foo ( QString *a, QString *b ) { }                                             //II
foo ( QString &amp a , QString &amp b) { }                            //III
foo ( const QString &amp a, const QString &amp b ) { }          //IV

First call is, pass by value or call by value and the rest are pass by reference or call by reference.


So what is the big difference between call by value and call by reference
Call by value : Function receives the copy of the original object. So whatever we do inside the function, it affects only this copy. It doesn't make any changes on the original one. But call by reference , (the second method), it passes the pointers, and so the changes can be made to the original object.
In both the 1 and II actual parameters are copied to the formal parameters, but the difference is, in I, the object is copied and in II only address to that object is copied, not the object. So we saved the time to copy ,and so as the memory.

What is '&' doing  here!  in III and in IV ?
    formal parameters = actual parameters; but
    & formal parameters = * actual parameters!!!!
Whats the logic?!
& is Commonly mistaken as "address of" operator. Well, anywhere else it is "address of" but  here its different. It is an indication to compiler to make it call by reference. Even though pointers(address) are not passed, here no copy is made and original object is accessed! Effectively same as II.

So what if i want no copy but also No access to the originals?
Simple, just add const as in IV!

----------------------------------------------------------------------------------------------------

Now about Qt. All the above are same in Qt. In addition ,but a smarter step is there.Implicit sharing or copy-on-write. Suppose a call is made as in I, Qt doesn't create any copy as long as the method don't change the values.The data is shared among objects , implicitly. A deep copy is created only when there are some modifications! And this is COW, Copy-On-Write.