Brad Howes wrote:
> On Dec 7, 2009, at 4:31 PM, Stephen Jackson wrote:
>> I'm sorry but both of these options permit mutation of the object
>> pointed at, as demonstrated by the following code.
>> The commented out options 1 & 2 both compile, permit the increment and print
>> The uncommented version is what you need to throw a compile-time error on
>> #include <QtGui>
>> #include <iostream>
>> // 1. void foo(const QSharedPointer<int> p)
>> // 2. void foo(const QSharedPointer<int> & p)
>> void foo(const QSharedPointer<const int> p)
> Doh! Thanks. In haste I forgot the second const. I will note that you don't
> need the first const if missing an '&' since you are working with a local
> variable anyway.
> void foo(const QSharedPointer<const int>&p)
> void foo(QSharedPointer<const int> p)
The first one is preferable since it does not cause the reference count
to be incremented on entry to the function and decremented on exit from
the function. The second way does the increment/decrement since it makes
a temporary local copy of the shared pointer.
Also, since the reference counting is atomic this can get expensive if
your function is called many many times.
Qt-interest mailing list