Suppose I have a vector of data generated from some API. It's for plotting only.
vector<double> LineData = generateData();
I also have a class that do the plotting and manage the data
class LineChart
{
public:
LineChart(vector* data):m_data{data}{}
vector<double> *m_data;
void plot();
};
LineChart lc {&LineData}
Since the vector is only for plotting, I want the class to take the full ownership. So I want to change the raw pointer to a unique_ptr.
Normally, a unique_ptr is created from new or make_unique,instead of an existing object. I could make a copy of the vector, then move it to the class.
unique_ptr<vector> data = make_unique<vector>(LineData)
But there is unnecessary overhead to copy the vector, since I have the data already. Also, the original vector becomes duplicated on the stack and does nothing.
So in situation like this, are there some efficient ways to pass the ownership of existing object to the class? What's the best practice?