[Kde-pim] KCalCore Ptr typedefs and constness

David Jarvie djarvie at kde.org
Thu Aug 19 17:42:58 BST 2010


On Thu, August 19, 2010 4:29 pm, Sérgio Martins wrote:
> 2010/8/19 David Jarvie <djarvie at kde.org>
>
>> I wonder if in addition to such typedefs as KCalCore::Event::Ptr, there
>> should also be
>>
>> typedef QSharedPointer<const KCalCore::Event> ConstPtr;
>>
>> AFAICS, currently it isn't possible using the Ptr types to have a
>> pointer
>> to const data. Specifying
>>
>> const Ptr
>>
>> will only make the pointer const, not the data pointed to.
>>
>> Or is QSharedPointer much cleverer than I imagine?
>>
>
> Hello David,
>
>
> Unfortunately it isn't as simple as adding a typedef.
>
> ConstPtr will give you a false sense of security and can be breached when
> you access getters which return non-const pointers:
>
> ConstPtr incidence( someIncidence );
> Person *person = incidence->organizer();
> person->setName( "foo" ); //ups
>
> We would have to duplicate(overload) every function that returns a pointer
> to avoid this.

But isn't this the same as when you use a const pointer instead of
ConstPtr? E.g. the following use of a const pointer works exactly the same
as your example:

const Incidence* incidence = someIncidence;
Person* person = incidence->organizer();
person->setName( "foo" );

What a const pointer, or ConstPtr, does is to prevent modification of the
object's direct members by calling the object's own methods. It also has
the important function to serve as a hint to be careful when calling
methods on pointers returned by the object's methods.

I think that not having a ConstPtr is a step backwards in that there is no
way of declaring a function parameter as a const pointer. It therefore
isn't possible when passing a pointer to a function to provide even a hint
as to whether the function will alter the object pointed to (other than by
reading the function's code).

All in all, I think the ability to declare a pointer as const while using
Ptr/ConstPtr is necessary. I'm happy to provide code for this if it's
agreed in principle.

-- 
David Jarvie.
KDE developer.
KAlarm author - http://www.astrojar.org.uk/kalarm

_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/


More information about the kde-pim mailing list