T& f() { // some code ... }
const T& f() const { // some code ... }
I\'ve seen this a couple of times now (in the introductory book I\'ve b
There is a very nice example in the Qt framework.
Take a look at the QImage class.
There are two public functions:
const uchar* scanLine (int i) const;
uchar* scanLine (int i);
The first one is for read access only. The second one is for the case that you want to modify the scan line.
Why is this distinction important? Because Qt uses implicit data sharing. This means, QImage does not immediately perform a deep copy if you do something like this:
QImage i1, i2;
i1.load("image.bmp");
i2 = i1; // i1 and i2 share data
Instead, the data is copied only and only if you call a function that actually modifies one of the two images, like the non-const scanLine.