Behaviour of const KSharedPtrs

Richard Smith kde at
Fri Nov 12 23:45:59 GMT 2004


I noticed something rather odd about KSharedPtr<T>: 'operator-> const' and 
'get() const' return a const T* rather than the expected T*, and 'operator* 
const' returns a const T& rather than the expected T&. This came up in Kopete 
in the context of replacing a Private*d (with reference counting we'd 
implemented ourselves) with a KSharedPtr<Private>; in a const member function 
the code relied on *d being non-const, and once the Private pointer was 
changed to a KSharedPtr the code stopped compiling.

Now, is this a bug? I think so. KSharedPtr<T> is, after all, supposed to to 
have pointer-like semantics. Pointers don't behave this way. 
boost::shared_ptr doesn't behave this way. And if I really want this 
behaviour I can use a KSharedPtr<const T> instead.

On the other hand, it fixes one of the problems with using d pointers: 
constness of member functions is not enforced (since although you can't 
change d in a const member function, you can change *d). Maybe this behaviour 
is intentional because of that very issue.

So, does anyone know if this is intentional or a bug?

More information about the kde-core-devel mailing list