MCPcopy Create free account
hub / github.com/pybind/pybind11 / Vector2

Class Vector2

tests/test_operator_overloading.cpp:18–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16#include <functional>
17
18class Vector2 {
19public:
20 Vector2(float x, float y) : x(x), y(y) { print_created(this, toString()); }
21 Vector2(const Vector2 &v) : x(v.x), y(v.y) { print_copy_created(this); }
22 Vector2(Vector2 &&v) noexcept : x(v.x), y(v.y) {
23 print_move_created(this);
24 v.x = v.y = 0;
25 }
26 Vector2 &operator=(const Vector2 &v) {
27 x = v.x;
28 y = v.y;
29 print_copy_assigned(this);
30 return *this;
31 }
32 Vector2 &operator=(Vector2 &&v) noexcept {
33 x = v.x;
34 y = v.y;
35 v.x = v.y = 0;
36 print_move_assigned(this);
37 return *this;
38 }
39 ~Vector2() { print_destroyed(this); }
40
41 std::string toString() const {
42 return "[" + std::to_string(x) + ", " + std::to_string(y) + "]";
43 }
44
45 Vector2 operator-() const { return Vector2(-x, -y); }
46 Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
47 Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
48 Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
49 Vector2 operator+(float value) const { return Vector2(x + value, y + value); }
50 Vector2 operator*(float value) const { return Vector2(x * value, y * value); }
51 Vector2 operator/(float value) const { return Vector2(x / value, y / value); }
52 Vector2 operator*(const Vector2 &v) const { return Vector2(x * v.x, y * v.y); }
53 Vector2 operator/(const Vector2 &v) const { return Vector2(x / v.x, y / v.y); }
54 Vector2 &operator+=(const Vector2 &v) {
55 x += v.x;
56 y += v.y;
57 return *this;
58 }
59 Vector2 &operator-=(const Vector2 &v) {
60 x -= v.x;
61 y -= v.y;
62 return *this;
63 }
64 Vector2 &operator*=(float v) {
65 x *= v;
66 y *= v;
67 return *this;
68 }
69 Vector2 &operator/=(float v) {
70 x /= v;
71 y /= v;
72 return *this;
73 }
74 Vector2 &operator*=(const Vector2 &v) {
75 x *= v.x;

Callers 4

operator-Method · 0.85
operator+Method · 0.85
operator*Method · 0.85
operator/Method · 0.85

Calls 2

print_copy_assignedFunction · 0.85
print_move_assignedFunction · 0.85

Tested by 4

operator-Method · 0.68
operator+Method · 0.68
operator*Method · 0.68
operator/Method · 0.68