I want to store QPoint
in an object, which should be released automatically.
#include <QCoreApplication>
#include <memory>
#include <QPoint>
using namespace std;
class MyWidget{
public:
vector<std::unique_ptr<QPoint>> _points;
void f(QPoint point){
std::unique_ptr<QPoint> pos(new QPoint(point));
_points.push_back(pos);
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyWidget wid;
wid.f(QPoint(0,0));
return a.exec();
}
The error message is:
F:\Qt\Qt5.5.0\Tools\mingw492_32\i686-w64-mingw32\include\c++\ext\new_allocator.h:120: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = QPoint; _Dp = std::default_delete]' { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } ^
Does it mean that I am not supposed to use unique_ptr
to store QPoint
?
QPoint
is a tiny object, using any pointer and heap allocation is entirely pointless. – dtech_points.push_back(std::make_unique<QPoint>(point));
. – Kerrek SBboost::ptr_vector
correctly propagates constness, but it would be an awful overkill here. So, yeah, const reference, imho. @Zen – MasterAler