Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Qt where's the std::unique_ptr Qt version?

Tags:

c++

qt

qt5

So when programming in Qt I like to go with the Qt implementation as far as possible. As far as I've seen there's no Qt version of the std::unique_ptr or is there?

What would be the alternatives that produce the "same" result in Qt if it doesn't exist?

like image 755
deW1 Avatar asked Sep 11 '14 11:09

deW1


People also ask

What is std :: unique_ptr?

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.

How to transfer ownership unique_ ptr?

In C++11 we can transfer the ownership of an object to another unique_ptr using std::move() . After the ownership transfer, the smart pointer that ceded the ownership becomes null and get() returns nullptr.

Can unique_ptr be null?

Creating a unique pointer as std::unique_ptr<T> myptr; doesn't allocate any memory, so you don't need to construct it and pass nullptr at all. So you can just create one and return it. nullptr is a keyword new in C++11 for better support of null pointers. unique_ptr can be constructed from nullptr , even implicitly.

How do you create a unique pointer in C++?

auto ptr = make_unique<int>(); // Create a new unique_ptr object. auto ptr = make_unique<int>(); The dynamically allocated object is destroyed when the created unique pointer object is destroyed.


1 Answers

The Qt equivalent to std::unique_ptr is QScopedPointer.

It's significantly less useful as it doesn't support move semantics, making it closer in utility to C++03 std::auto_ptr (Why is auto_ptr being deprecated?).

like image 145
ecatmur Avatar answered Oct 14 '22 11:10

ecatmur