From b63d16064a727d154e47ffbbb66c41be349cfd09 Mon Sep 17 00:00:00 2001 From: darcy Date: Tue, 29 Oct 2024 16:51:56 +1100 Subject: [PATCH 1/4] Remove `explicit` specifier from `Coordinate` constructor --- include/mcpp/util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mcpp/util.h b/include/mcpp/util.h index e503b43..b11e5ff 100644 --- a/include/mcpp/util.h +++ b/include/mcpp/util.h @@ -23,7 +23,7 @@ struct Coordinate { * @param y The y-coordinate. Default is 0. * @param z The z-coordinate. Default is 0. */ - explicit Coordinate(int x = 0, int y = 0, int z = 0); + Coordinate(int x = 0, int y = 0, int z = 0); /** * @brief Constructs a Coordinate object with double values. From 0b139d4b632755f6ce22b1b69c55b1246c36791d Mon Sep 17 00:00:00 2001 From: darcy Date: Tue, 29 Oct 2024 16:56:56 +1100 Subject: [PATCH 2/4] Remove default parameter values for `Coordinate` constructor Require either 0 or 3 arguments, disallowing `Coordinate(some_x)` and `Coordinate(some_x, some_y)` --- include/mcpp/util.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/include/mcpp/util.h b/include/mcpp/util.h index b11e5ff..4fccee8 100644 --- a/include/mcpp/util.h +++ b/include/mcpp/util.h @@ -19,11 +19,16 @@ struct Coordinate { /** * @brief Constructs a Coordinate object with integer values. * - * @param x The x-coordinate. Default is 0. - * @param y The y-coordinate. Default is 0. - * @param z The z-coordinate. Default is 0. + * @param x The x-coordinate. + * @param y The y-coordinate. + * @param z The z-coordinate. */ - Coordinate(int x = 0, int y = 0, int z = 0); + Coordinate(int x, int y, int z); + + /** + * @brief Constructs a Coordinate object with zero values. + */ + Coordinate() : x(0), y(0), z(0) {} /** * @brief Constructs a Coordinate object with double values. From 90a05e2ab31913a34911e28a2b913898a78d3a51 Mon Sep 17 00:00:00 2001 From: darcy Date: Tue, 29 Oct 2024 16:59:19 +1100 Subject: [PATCH 3/4] Use initializer lists for `Coordinate` constructors --- include/mcpp/util.h | 6 ++++-- src/util.cpp | 12 ------------ 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/include/mcpp/util.h b/include/mcpp/util.h index 4fccee8..157be05 100644 --- a/include/mcpp/util.h +++ b/include/mcpp/util.h @@ -23,7 +23,7 @@ struct Coordinate { * @param y The y-coordinate. * @param z The z-coordinate. */ - Coordinate(int x, int y, int z); + Coordinate(int x, int y, int z) : x(x), y(y), z(z) {} /** * @brief Constructs a Coordinate object with zero values. @@ -37,7 +37,9 @@ struct Coordinate { * @param y The y-coordinate as a double. * @param z The z-coordinate as a double. */ - Coordinate(double x, double y, double z); + Coordinate(double x, double y, double z) + : x(static_cast(x)), y(static_cast(y)), + z(static_cast(z)) {} /** * @brief Adds two Coordinate objects. diff --git a/src/util.cpp b/src/util.cpp index f52aa55..bf82f7c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -5,18 +5,6 @@ namespace mcpp { -Coordinate::Coordinate(int x, int y, int z) { - this->x = x; - this->y = y; - this->z = z; -} - -Coordinate::Coordinate(double x, double y, double z) { - this->x = static_cast(x); - this->y = static_cast(y); - this->z = static_cast(z); -} - Coordinate Coordinate::operator+(const Coordinate& obj) const { Coordinate result; result.x = this->x + obj.x; From e6430db5915697cf0794de43fb6514fe044ee377 Mon Sep 17 00:00:00 2001 From: darcy Date: Tue, 29 Oct 2024 17:00:29 +1100 Subject: [PATCH 4/4] Add `constexpr` specifier for `Coordinate` constructors --- include/mcpp/util.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/mcpp/util.h b/include/mcpp/util.h index 157be05..ae36fd6 100644 --- a/include/mcpp/util.h +++ b/include/mcpp/util.h @@ -23,12 +23,12 @@ struct Coordinate { * @param y The y-coordinate. * @param z The z-coordinate. */ - Coordinate(int x, int y, int z) : x(x), y(y), z(z) {} + constexpr Coordinate(int x, int y, int z) : x(x), y(y), z(z) {} /** * @brief Constructs a Coordinate object with zero values. */ - Coordinate() : x(0), y(0), z(0) {} + constexpr Coordinate() : x(0), y(0), z(0) {} /** * @brief Constructs a Coordinate object with double values. @@ -37,7 +37,7 @@ struct Coordinate { * @param y The y-coordinate as a double. * @param z The z-coordinate as a double. */ - Coordinate(double x, double y, double z) + constexpr Coordinate(double x, double y, double z) : x(static_cast(x)), y(static_cast(y)), z(static_cast(z)) {}