c++ - Qt::make_shared for creating QSharedPtr as std::make_shared for creating std::shared_ptr -
As stated in the Barjini Stroustrup's "A Tour of C ++", and a known C ++ 14 practice As such, someone should avoid the naked code in the However, it is not possible to use these routines for non-standard smart pointers, such as QT's own memory management model (with parent), but also smart pointer class like Provides My question is: Is not it convenient to create Qt analog of or to create this, and it can be used like this: Is there any warning for this approach? Is there any publicly known use of this approach? UPDATE I assume that As strongly as the features of Qt 5.1 already provides its own implementation to create smart pointers . That stable member function can be used as follows: But note the details: Note: This function is available with the C ++ 11 compiler which is correct Supports forwarding an arbitrary number of arguments If the compiler does not support the required C ++ 11 features, then you should use the overload which calls the default constructor. This special, forwarding task single system stored object and Both can assign to a memory call and reference counter . The memory allocation is separated from the context of calling, so when you create another object, avoid the memory leak in case of an exception call a function in the example (Where a compiler is free to select the order in which the argument is evaluated). Consider: That is, if the compiler first new and
delete . Provides
std :: make_shared and
std :: make_unique to create smart pointers to store allocated items in the standard library.
QSharedPointer and
QPointer (although later it is not actually a brochure indicator).
std :: make_shared ? To like it,
QSharedPtr :
Namespace Qt {template & lt; Class T, squares ... Args & gt; QSharedPointer & LT; T & gt; Make_shared (erg & amp; ... args) {return QSharedPointer & lt; T & gt; (New t (std :: forward & lt; ergos & gt; (args) ...)); }}
QPointer :
named place QT {template
automatically pCancelButton = Qt :: make_ptr & lt; QPushButton & gt; ("Cancel", this);
Qt :: make_ptr is useful unless
QPointer is useful, because it
New hides the operator and ensures that
new is only for certain things that can be found on
QObject . Qt users do too many
new s, but in this way we can ensure that the
new is used only in the QT context, any debate on this idea?
make_shared The right forwarding feature , which is available only after C ++ 11 and introduces universal (forwarding) reference , saying that without correct forwarding Using this function may be disabled, Qt is significantly older than the C ++ standard recently, so it was not available until Qt 5.1 (as you have to do with QT's internal pre-PC
auto ptr = QSharedPointer & lt; QPushButton & gt; :: Create ("Cancel", this);
new
new
:
foo (QSharedPointer & lt; QPushButton & gt; (New QPushButton ("Cancel", this)), MayThrow ());
the new QPushButton ("Cancel" ", This) expression, and then calls the
Mayrhrow () function before calling the constructor of
QSharedPointer , if you enter
MayThrow ( ) The function throws an exception, so you can leak memory.
Comments
Post a Comment