[Kde-pim] KCalCore Ptr typedefs and constness

David Jarvie djarvie at kde.org
Fri Aug 20 13:03:25 BST 2010


On Fri, August 20, 2010 10:24 am, Sérgio Martins wrote:
> On Thu, Aug 19, 2010 at 4:42 PM, David Jarvie <djarvie at kde.org> wrote:
>
>> 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: (...)
>>
>
> The difference is the typedef. I see the act of creating the typedef as
> advertising people to use something that only protects half object.
>
> Without the typedef they still can write QSharedPointer<const Event> and
> use
> it at their own risk.
>
> I would rather have the overloads first, then provide the typedef.

I think that many of the method signatures in kcalcore are misleading
because they use for example 'const Event::Ptr&' which gives the initial
impression of being equivalent to 'const Event*', but is actually
equivalent to 'Event* const', i.e. only the pointer is constant, not the
object pointed to. I think it would be a bit clearer to pass 'const
Event::ConstPtr&' which *would* be equivalent to 'const Event*'.

-- 
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