From 1ed684f3a1dfaa05eb325a9fac8ab99f23995a4a Mon Sep 17 00:00:00 2001 From: Haibo Date: Mon, 12 Nov 2018 22:56:47 +0800 Subject: [PATCH] fix: operator +-*/ should return const object --- core/base/Size.cpp | 10 ++++----- core/base/Size.h | 10 ++++----- core/base/time.cpp | 52 ++++++++++++++++++++++---------------------- core/base/time.h | 52 ++++++++++++++++++++++---------------------- core/math/Matrix.hpp | 2 +- core/math/vector.hpp | 10 ++++----- 6 files changed, 68 insertions(+), 68 deletions(-) diff --git a/core/base/Size.cpp b/core/base/Size.cpp index 9f669a83..3460b86b 100644 --- a/core/base/Size.cpp +++ b/core/base/Size.cpp @@ -40,27 +40,27 @@ namespace easy2d height = other.height; } - Size Size::operator+(const Size & other) const + const Size Size::operator+(const Size & other) const { return Size(width + other.width, height + other.height); } - Size Size::operator-(const Size & other) const + const Size Size::operator-(const Size & other) const { return Size(width - other.width, height - other.height); } - Size Size::operator*(float val) const + const Size Size::operator*(float val) const { return Size(width * val, height * val); } - Size Size::operator/(float val) const + const Size Size::operator/(float val) const { return Size(width / val, height / val); } - Size Size::operator-() const + const Size Size::operator-() const { return Size(-width, -height); } diff --git a/core/base/Size.h b/core/base/Size.h index 71b14fbc..a2290023 100644 --- a/core/base/Size.h +++ b/core/base/Size.h @@ -47,11 +47,11 @@ namespace easy2d const Size& other ); - Size operator + (const Size & other) const; - Size operator - (const Size & other) const; - Size operator * (float val) const; - Size operator / (float val) const; - Size operator - () const; + const Size operator + (const Size & other) const; + const Size operator - (const Size & other) const; + const Size operator * (float val) const; + const Size operator / (float val) const; + const Size operator - () const; bool operator== (const Size& other) const; inline operator D2D1_SIZE_F () const diff --git a/core/base/time.cpp b/core/base/time.cpp index 62bf2c3a..30666abf 100644 --- a/core/base/time.cpp +++ b/core/base/time.cpp @@ -66,12 +66,12 @@ namespace easy2d return !!dur_since_epoch_.Milliseconds(); } - TimePoint TimePoint::operator+(const Duration & dur) const + const TimePoint TimePoint::operator+(const Duration & dur) const { return TimePoint(dur_since_epoch_ + dur); } - TimePoint TimePoint::operator-(const Duration & dur) const + const TimePoint TimePoint::operator-(const Duration & dur) const { return TimePoint(dur_since_epoch_ - dur); } @@ -88,7 +88,7 @@ namespace easy2d return (*this); } - Duration TimePoint::operator-(const TimePoint & other) const + const Duration TimePoint::operator-(const TimePoint & other) const { return dur_since_epoch_ - other.dur_since_epoch_; } @@ -246,67 +246,67 @@ namespace easy2d return milliseconds_ <= other.milliseconds_; } - Duration Duration::operator+(const Duration & other) const + const Duration Duration::operator+(const Duration & other) const { return Duration(milliseconds_ + other.milliseconds_); } - Duration Duration::operator-(const Duration & other) const + const Duration Duration::operator-(const Duration & other) const { return Duration(milliseconds_ - other.milliseconds_); } - Duration Duration::operator-() const + const Duration Duration::operator-() const { return Duration(-milliseconds_); } - Duration Duration::operator*(int val) const + const Duration Duration::operator*(int val) const { return Duration(milliseconds_ * val); } - Duration Duration::operator/(int val) const + const Duration Duration::operator/(int val) const { return Duration(milliseconds_ / val); } - Duration easy2d::time::Duration::operator*(unsigned long long val) const + const Duration easy2d::time::Duration::operator*(unsigned long long val) const { return Duration(static_cast(milliseconds_ * val)); } - Duration easy2d::time::Duration::operator/(unsigned long long val) const + const Duration easy2d::time::Duration::operator/(unsigned long long val) const { return Duration(static_cast(milliseconds_ / val)); } - Duration Duration::operator*(float val) const + const Duration Duration::operator*(float val) const { return Duration(static_cast(milliseconds_ * val)); } - Duration Duration::operator/(float val) const + const Duration Duration::operator/(float val) const { return Duration(static_cast(milliseconds_ / val)); } - Duration Duration::operator*(double val) const + const Duration Duration::operator*(double val) const { return Duration(static_cast(milliseconds_ * val)); } - Duration Duration::operator*(long double val) const + const Duration Duration::operator*(long double val) const { return Duration(static_cast(milliseconds_ * val)); } - Duration Duration::operator/(double val) const + const Duration Duration::operator/(double val) const { return Duration(static_cast(milliseconds_ / val)); } - Duration Duration::operator/(long double val) const + const Duration Duration::operator/(long double val) const { return Duration(static_cast(milliseconds_ / val)); } @@ -383,52 +383,52 @@ namespace easy2d return (*this); } - Duration easy2d::time::operator*(int val, const Duration & dur) + const Duration easy2d::time::operator*(int val, const Duration & dur) { return dur * val; } - Duration easy2d::time::operator*(unsigned long long val, const Duration & dur) + const Duration easy2d::time::operator*(unsigned long long val, const Duration & dur) { return dur / val; } - Duration easy2d::time::operator/(int val, const Duration & dur) + const Duration easy2d::time::operator/(int val, const Duration & dur) { return dur / val; } - Duration easy2d::time::operator/(unsigned long long val, const Duration & dur) + const Duration easy2d::time::operator/(unsigned long long val, const Duration & dur) { return dur * val; } - Duration easy2d::time::operator*(float val, const Duration & dur) + const Duration easy2d::time::operator*(float val, const Duration & dur) { return dur * val; } - Duration easy2d::time::operator/(float val, const Duration & dur) + const Duration easy2d::time::operator/(float val, const Duration & dur) { return dur / val; } - Duration easy2d::time::operator*(double val, const Duration & dur) + const Duration easy2d::time::operator*(double val, const Duration & dur) { return dur * val; } - Duration easy2d::time::operator/(double val, const Duration & dur) + const Duration easy2d::time::operator/(double val, const Duration & dur) { return dur / val; } - Duration easy2d::time::operator*(long double val, const Duration & dur) + const Duration easy2d::time::operator*(long double val, const Duration & dur) { return dur * val; } - Duration easy2d::time::operator/(long double val, const Duration & dur) + const Duration easy2d::time::operator/(long double val, const Duration & dur) { return dur / val; } diff --git a/core/base/time.h b/core/base/time.h index e1ecf646..08d67265 100644 --- a/core/base/time.h +++ b/core/base/time.h @@ -70,19 +70,19 @@ namespace easy2d bool operator< (const Duration &) const; bool operator<= (const Duration &) const; - Duration operator + (const Duration &) const; - Duration operator - (const Duration &) const; - Duration operator - () const; - Duration operator * (int) const; - Duration operator * (unsigned long long) const; - Duration operator * (float) const; - Duration operator * (double) const; - Duration operator * (long double) const; - Duration operator / (int) const; - Duration operator / (unsigned long long) const; - Duration operator / (float) const; - Duration operator / (double) const; - Duration operator / (long double) const; + const Duration operator + (const Duration &) const; + const Duration operator - (const Duration &) const; + const Duration operator - () const; + const Duration operator * (int) const; + const Duration operator * (unsigned long long) const; + const Duration operator * (float) const; + const Duration operator * (double) const; + const Duration operator * (long double) const; + const Duration operator / (int) const; + const Duration operator / (unsigned long long) const; + const Duration operator / (float) const; + const Duration operator / (double) const; + const Duration operator / (long double) const; Duration& operator += (const Duration &); Duration& operator -= (const Duration &); @@ -97,16 +97,16 @@ namespace easy2d Duration& operator /= (double); Duration& operator /= (long double); - friend Duration operator* (int, const Duration &); - friend Duration operator* (unsigned long long, const Duration &); - friend Duration operator* (float, const Duration &); - friend Duration operator* (double, const Duration &); - friend Duration operator* (long double, const Duration &); - friend Duration operator/ (int, const Duration &); - friend Duration operator/ (unsigned long long, const Duration &); - friend Duration operator/ (float, const Duration &); - friend Duration operator/ (double, const Duration &); - friend Duration operator/ (long double, const Duration &); + friend const Duration operator* (int, const Duration &); + friend const Duration operator* (unsigned long long, const Duration &); + friend const Duration operator* (float, const Duration &); + friend const Duration operator* (double, const Duration &); + friend const Duration operator* (long double, const Duration &); + friend const Duration operator/ (int, const Duration &); + friend const Duration operator/ (unsigned long long, const Duration &); + friend const Duration operator/ (float, const Duration &); + friend const Duration operator/ (double, const Duration &); + friend const Duration operator/ (long double, const Duration &); friend std::wostream& operator<< (std::wostream &, const Duration &); friend std::wistream& operator>> (std::wistream &, Duration &); @@ -161,13 +161,13 @@ namespace easy2d // ΚΗ·ρΚΗΑγΚ± bool IsZero() const; - TimePoint operator + (const Duration &) const; - TimePoint operator - (const Duration &) const; + const TimePoint operator + (const Duration &) const; + const TimePoint operator - (const Duration &) const; TimePoint& operator += (const Duration &); TimePoint& operator -= (const Duration &); - Duration operator - (const TimePoint &) const; + const Duration operator - (const TimePoint &) const; TimePoint& operator = (const TimePoint &) E2D_NOEXCEPT; TimePoint& operator = (TimePoint &&) E2D_NOEXCEPT; diff --git a/core/math/Matrix.hpp b/core/math/Matrix.hpp index 34bf97af..7fb25aa9 100644 --- a/core/math/Matrix.hpp +++ b/core/math/Matrix.hpp @@ -62,7 +62,7 @@ namespace easy2d this->_32 = _32; } - inline Matrix operator*(const Matrix &matrix) const + inline const Matrix operator*(const Matrix &matrix) const { return Matrix( _11 * matrix._11 + _12 * matrix._21, diff --git a/core/math/vector.hpp b/core/math/vector.hpp index 2e8a0c96..bdd5583d 100644 --- a/core/math/vector.hpp +++ b/core/math/vector.hpp @@ -60,27 +60,27 @@ namespace easy2d return Vector2(x - v.x, y - v.y).Length(); } - inline Vector2 operator + (const Vector2 & other) const + inline const Vector2 operator + (const Vector2 & other) const { return Vector2(x + other.x, y + other.y); } - inline Vector2 operator - (const Vector2 & other) const + inline const Vector2 operator - (const Vector2 & other) const { return Vector2(x - other.x, y - other.y); } - inline Vector2 operator * (float val) const + inline const Vector2 operator * (float val) const { return Vector2(x * val, y * val); } - inline Vector2 operator / (float val) const + inline const Vector2 operator / (float val) const { return Vector2(x / val, y / val); } - inline Vector2 operator - () const + inline const Vector2 operator - () const { return Vector2(-x, -y); }