[Interest] assigning to QSharedPointer

Thiago Macieira thiago.macieira at intel.com
Tue May 30 07:33:47 CEST 2017

On segunda-feira, 29 de maio de 2017 17:50:23 PDT Hamish Moffatt wrote:
> Which of the following three ways to create a new object and manage it
> with QSharedPointer is preferable?
> class Foo;
> QSharedPointer<Foo> f1, f2, f3; // class members
> f1.reset(new Foo);

Works fine.

    inline void reset(T *t)
    { QSharedPointer copy(t); swap(copy); }

> f2 = QSharedPointer<Foo>::create(); // or even f2 = f2.create()

Also works fine and does the same thing:

    QSharedPointer &operator=(QSharedPointer &&other) Q_DECL_NOTHROW
        QSharedPointer moved(std::move(other));
        return *this;

The difference is the use of create().

> f3.swap(QSharedPointer<Foo>::create()); // or f3.swap(f3.create()))

This one doesn't compile.

> I find #2 the easiest to read. Do #2 and #3 perform any differently?

A simple read of the source code would have answered.

> Anything else to consider?

Yes: use create().

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

More information about the Interest mailing list