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

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
Development mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to