From e6595cf9c527e97198806c48aa0a14ae8e895e87 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 21 Jan 2019 00:39:13 +0100 Subject: Add member operator =, +=, -=, *= overloads for basic_vec --- example.cpp | 6 ++++++ mmvec.hpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/example.cpp b/example.cpp index e72d65b..3a00f58 100644 --- a/example.cpp +++ b/example.cpp @@ -21,6 +21,8 @@ int main(int argc, char *argv[]) { std::cout << "u * v = " << u * v << std::endl; std::cout << std::endl; + u += v; + // three dimensional vectors std::cout << "three dimensional (double) vectors" << std::endl; @@ -37,6 +39,8 @@ int main(int argc, char *argv[]) { std::cout << "spherical(a) = " << a.spherical() << std::endl; std::cout << std::endl; + a -= b; + // two dimensional vector std::cout << "two dimensional (complex) vectors" << std::endl; @@ -52,5 +56,7 @@ int main(int argc, char *argv[]) { std::cout << "polar(j) = " << j.polar() << std::endl; std::cout << std::endl; + j *= 10; + return 0; } diff --git a/mmvec.hpp b/mmvec.hpp index 5173e85..0f21d22 100644 --- a/mmvec.hpp +++ b/mmvec.hpp @@ -53,9 +53,23 @@ struct mm::basic_vec : public std::array { template basic_vec(const basic_vec& other); T length() const; + + template + basic_vec& operator=(const mm::basic_vec& other); + + template + basic_vec& operator+=(const mm::basic_vec& other); + + template + basic_vec& operator-=(const mm::basic_vec& other); + + + basic_vec& operator*=(const T& scalar); }; +// member functions for basic_vec + template mm::basic_vec::basic_vec() : std::array() { this->fill(basic_vec::null_element); @@ -76,13 +90,8 @@ template mm::basic_vec::basic_vec(const mm::basic_vec& other) { // construct with empty values basic_vec(); - - static_assert( - d >= n, - "cannot copy higher dimensional vector into a smaller one" - ); - - std::copy(other.begin(), other.end(), this->begin()); + // uses operator= + *this = other; } template @@ -95,6 +104,45 @@ T mm::basic_vec::length() const { )); } + +// memeber operator overloads for basic_vec + +template +template +mm::basic_vec& mm::basic_vec::operator=(const mm::basic_vec& other) { + static_assert( + d >= n, + "cannot copy higher dimensional vector into a smaller one" + ); + + std::copy(other.begin(), other.end(), this->begin()); + + return *this; +} + +template +template +mm::basic_vec& mm::basic_vec::operator+=(const mm::basic_vec& other) { + *this = *this + other; + return *this; +} + +template +template +mm::basic_vec& mm::basic_vec::operator-=(const mm::basic_vec& other) { + *this = *this - other; + return *this; +} + +template +mm::basic_vec& mm::basic_vec::operator*=(const T& scalar) { + *this = *this * scalar; + return *this; +} + + +// operator overloads for basic_vec + template mm::basic_vec operator+(const mm::basic_vec& rhs, const mm::basic_vec& lhs) { mm::basic_vec out; @@ -146,7 +194,9 @@ std::ostream& operator<<(std::ostream& os, const mm::basic_vec& v) { return os; } -// actual classes to use in your code + +// actual vectors to use in your code + template class mm::vec: public mm::basic_vec { public: @@ -156,7 +206,10 @@ public: vec(const basic_vec& other) : basic_vec(other) {} }; + // three dimensional specialization with a static cross product +// TODO: specialize operator+ for spherical coordinates + template class mm::vec3 : public mm::basic_vec { public: @@ -211,7 +264,10 @@ mm::vec3 mm::vec3::cross(const vec3& rhs, const vec3& lhs) { return res; } + // two dimensional specialization with a polar conversion +// TODO: specialize operator+ for polar coordinates + template class mm::vec2: public mm::basic_vec { public: -- cgit v1.2.1