In my code
std::unique_ptr<QNetworkAccessManager> myNetworkAccessManager;
...
myNetworkAccessManager.reset(new QNetworkAccessManager(this));
QObject::connect(myNetworkAccessManager.get(), SIGNAL(finished(QNetworkReply *)), this, SLOT(OnNetworkFinished(QNetworkReply *)));
code in question: myNetworkAccessManager.get() in connect()
My question is what is the best way to do it?
I was reading the article here article where it says under Legacy Code:
Calling get() returns a pointer to the underlying method. You really want to avoid calling this if you can, because as soon as you release that raw pointer into the wild, you have lost much of the advantage you achieved by switching to unique_ptr
Update
If I changed it to use If I used QScopedPointer, my code would be as follows:
QScopedPointer<QNetworkAccessManager> myNetworkAccessManager;
...
myNetworkAccessManager.reset(new QNetworkAccessManager(this));
QObject::connect(myNetworkAccessManager.data(), SIGNAL(finished(QNetworkReply *)), this, SLOT(OnNetworkFinished(QNetworkReply *)));
would this be the correct solution then:
connect(myNetworkAccessManager.data()
Update 2
Reading this link stackoverflowit seems that using data() is the correct solution. Which mean that using get() from the stl is correct also.
Qt has it's own memory management mechanism, so using
std::unique_ptr
for objects that are handled with that mechanism is not correct. I would advice to simply be sure to define parent-child relations correctly and let Qt handle that for you.