[Development] final value classes: some background information and plans
Thiago Macieira
thiago.macieira at intel.com
Wed Mar 7 17:19:48 CET 2012
On quarta-feira, 7 de março de 2012 17.17.47, Alex Strickland wrote:
> On 2012/03/07 03:25 PM, Marc Mutz wrote:
> > You shouldn't look at the Qt-project developers when discussing
> > interfaces.
> > You should look a the Qt programmers in the trenches that happily write
> > QColor * c = new QColor(...), as blissfully ignorant of the resource leak
> > as they are of Sutter/Alexandrescu. So, Qt can either default to safety
> > or it can default to the sharp edge. If you think the latter is
> > acceptable, I urge you to run a Qt Open Enrollment training as an
> > eye-opener. Hell, I inherited QPtrList publicly when I started out, and I
> > would have been glad if the compiler told me I shouldn't do that.
>
> I'll bite, what is the problem with declaring c this way if I promise to
> delete it?
Because even if you're not leaking it, you're using a non-optimal way of doing
things. Since QColor is a value-type class (copyable, comparable, default-
constructible, destructible, no polymorphism), using new there is a waste of
resources -- unnecessary indirection and overhead. In addition to calling the
constructor (most of which are inline), the syntax above needs to allocate
memory, which is a comparatively expensive and non-deterministic operation. It
also adds overhead, an indirection of memory access and potentially a new
cache miss.
All of the value-type classes should be used as pointers if and only if, under
similar circumstances, you'd use a pointer to int too.
So, tell me. Why would you write?
int *i = new int(42);
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Intel Sweden AB - Registration Number: 556189-6027
Knarrarnäsgatan 15, 164 40 Kista, Stockholm, Sweden
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/development/attachments/20120307/4af8ed25/attachment.sig>
More information about the Development
mailing list