Add assignment operators to some classes (#3937)

The implicitly defined assignment operator for classes having custom
defined copy constructor is deprecated.
This patch adds explicit assignment operators.
XDataSeries assignment operator now deletes XDataPoints pointed by
datapoints array in the target and creates new ones for the source,
this required a change in the way XDataSeries is used in JSON parser,
which was based on default assignment operator semantics.
This commit is contained in:
Alejandro Martinez
2024-02-09 10:02:39 -03:00
parent 81fc763a4f
commit 35433b4f65
4 changed files with 17 additions and 7 deletions

View File

@@ -41,11 +41,15 @@ public:
for (int i = 0; i<MAX_MEASURES; i++) values[i] = 0.0;
}
Measure(const Measure &other) {
*this = other;
}
Measure& operator=(const Measure &other) {
this->when = other.when;
this->comment = other.comment;
this->source = other.source;
this->originalSource = other.originalSource;
for (int i = 0; i<MAX_MEASURES; i++) this->values[i] = other.values[i];
return *this;
}
~Measure() {}

View File

@@ -262,11 +262,7 @@ xdata_list: xdata_series
| xdata_list ',' xdata_series
;
xdata_series: '{' xdata_items '}' { XDataSeries *add = new XDataSeries;
add->name=jc->xdataseries.name;
add->datapoints=jc->xdataseries.datapoints;
add->valuename=jc->xdataseries.valuename;
add->unitname=jc->xdataseries.unitname;
xdata_series: '{' xdata_items '}' { XDataSeries *add = new XDataSeries(jc->xdataseries);
jc->JsonRide->addXData(add->name, add);
// clear for next one

View File

@@ -41,6 +41,7 @@ public:
v3(double a, double b, double c) : m_t(a, b, c) {};
v3(const v3& o) : m_t(o.m_t) {}
v3& operator=(const v3& o) { m_t = o.m_t; return *this; }
double x() const { return std::get<0>(m_t); }
double y() const { return std::get<1>(m_t); }

View File

@@ -572,13 +572,17 @@ public:
string[i]="";
}
}
XDataPoint (const XDataPoint &other) {
XDataPoint(const XDataPoint &other) {
*this = other;
}
XDataPoint& operator=(const XDataPoint &other) {
this->secs=other.secs;
this->km=other.km;
for(int i=0; i<XDATA_MAXVALUES; i++) {
this->number[i]= other.number[i];
this->string[i]= other.string[i];
}
return *this;
}
double secs, km;
@@ -589,16 +593,21 @@ public:
class XDataSeries {
public:
XDataSeries() {}
XDataSeries(XDataSeries &other) {
XDataSeries(const XDataSeries& other) { *this = other; }
XDataSeries& operator=(const XDataSeries &other) {
name = other.name;
valuename = other.valuename;
unitname = other.unitname;
valuetype = other.valuetype;
// we need to delete objects pointed by the assignment target
foreach(XDataPoint *p, datapoints) delete p;
datapoints.clear();
// we need to create new objects since we are holding pointers to objects
// otherwise we would end up w/ multiple frees or dangling ptrs!
foreach (XDataPoint *p, other.datapoints) {
datapoints.push_back(new XDataPoint(*p));
}
return *this;
}
~XDataSeries() { foreach(XDataPoint *p, datapoints) delete p; }