From f319083926947307812c3baa8a534321e4549b8b Mon Sep 17 00:00:00 2001 From: HugoFara Date: Fri, 14 Aug 2026 15:34:22 +0200 Subject: [PATCH] refactor(core): declare copy-assignment on FFPoint and FFVector Both hand-write a copy-constructor and leave assignment to the compiler, so each class carries two independently maintained definitions of how to copy itself. They agree today. They stop agreeing the moment anyone adds a member: the generated assignment picks it up, the hand-written constructor does not, leaving `FFPoint b(a)` and `b = a` producing different objects. FFPoint carries every coordinate in the simulation. Writing the assignment operator out puts both next to each other, so a new member has to be added to both or the omission is visible in one file. Behaviour is unchanged: three doubles each, owning nothing. Contributes to #161, which is where this came to notice: 65 of its 428 warning lines were the missing operator. The issue attributes all 65 to FFPoint; 51 are, the other 14 are FFVector, which has the same shape. --- src/FFPoint.cpp | 10 ++++++++++ src/FFPoint.h | 3 +++ src/FFVector.cpp | 8 ++++++++ src/FFVector.h | 2 ++ 4 files changed, 23 insertions(+) diff --git a/src/FFPoint.cpp b/src/FFPoint.cpp index 0d21c849..a7a8cc73 100644 --- a/src/FFPoint.cpp +++ b/src/FFPoint.cpp @@ -32,6 +32,16 @@ FFPoint::~FFPoint(){ FFPoint::FFPoint(const FFPoint& p) : x(p.x), y(p.y), z(p.z) { // nothing else to do } +// Spelled out rather than left implicit: declaring the copy-constructor above +// deprecates the implicit assignment, so every `a = b` on an FFPoint raises +// -Wdeprecated-copy. The three coordinates own no memory, so copying them is +// exactly what the implicit version did. +FFPoint& FFPoint::operator=(const FFPoint& p){ + x = p.x; + y = p.y; + z = p.z; + return *this; +} // overloading operators const FFPoint operator+(const FFPoint& left, const FFPoint& right){ diff --git a/src/FFPoint.h b/src/FFPoint.h index bb3eb679..10bb8e00 100644 --- a/src/FFPoint.h +++ b/src/FFPoint.h @@ -40,6 +40,9 @@ class FFPoint { /*! \brief Copy-constructor * \param[in] 'p' : point to be copied */ FFPoint(const FFPoint& p); + /*! \brief Copy-assignment + * \param[in] 'p' : point to be copied */ + FFPoint& operator=(const FFPoint& p); /*! \brief overloaded operator + */ friend const FFPoint operator+(const FFPoint&, const FFPoint&); diff --git a/src/FFVector.cpp b/src/FFVector.cpp index cf50fbc2..abe772ec 100644 --- a/src/FFVector.cpp +++ b/src/FFVector.cpp @@ -44,6 +44,14 @@ FFVector::~FFVector() { FFVector::FFVector(const FFVector& v) : vx(v.vx), vy(v.vy), vz(v.vz){ // nothing else to do } +// Same reason as FFPoint::operator=: the copy-constructor above deprecates the +// implicit assignment, and the three components own no memory. +FFVector& FFVector::operator=(const FFVector& v){ + vx = v.vx; + vy = v.vy; + vz = v.vz; + return *this; +} // overloading operators const FFVector operator+(const FFVector& left, const FFVector& right){ diff --git a/src/FFVector.h b/src/FFVector.h index 7e91e296..a1301a2a 100644 --- a/src/FFVector.h +++ b/src/FFVector.h @@ -41,6 +41,8 @@ class FFVector { virtual ~FFVector(); /*! \brief Copy-constructor */ FFVector(const FFVector&); + /*! \brief Copy-assignment */ + FFVector& operator=(const FFVector&); /*! \brief overloaded operator + */ friend const FFVector operator+(const FFVector&, const FFVector&);