[Development] QDateTime addDays logic

Edward Welbourne edward.welbourne at qt.io
Thu Dec 13 13:58:20 CET 2018


Fausto Papandrea (13 December 2018 12:48)
> Hi, I would like to understand the logic of the addDays function of
> QDateTime.
>
> I mean, why doesn't it modify the calling object, but returns a copy of
> a new object instead?

At this point, it does what it does because it's done so for years
(since 5.0, at least) and changing it would break various compatibility
promises.  I guess the reason for it originally would be a general
preference for non-mutating methods; think of the add*() methods as
operator+() specialisations, rather than as operator+=().  If you need
to advance a QDateTime's day, you can always use

  when->setDate(when.date().addDays(n));

or simply

  when = when.addDays(n);

to achieve the mutating variant.

	Eddy.



More information about the Development mailing list