-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.cpp
71 lines (57 loc) · 1.56 KB
/
operators.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
struct Vector2
{
int x, y;
Vector2 operator+(const Vector2 &other) const
{
return Vector2{x + other.x, y + other.y};
}
Vector2 operator*(const Vector2 &other) const
{
return Vector2{x * other.x, y * other.y};
}
bool operator==(const Vector2 &other) const
{
return x == other.x && y == other.y;
}
bool operator!=(const Vector2 &other) const
{
return !(*this == other);
// note: "this" is a pointer to the current object
}
// ...
};
class ScopedPtr
{
public:
ScopedPtr(Vector2 *ptr) : m_Ptr(ptr) {}
~ScopedPtr() { delete m_Ptr; }
Vector2 *operator->() { return m_Ptr; } // makes code cleaner
const Vector2 *operator->() const { return m_Ptr; } // const version
private:
Vector2 *m_Ptr;
};
// operator overload
std::ostream &operator<<(std::ostream &stream, const Vector2 &other)
{
stream << other.x << ", " << other.y;
return stream;
}
int main()
{
Vector2 position = {1, 2};
Vector2 speed = {2, 3};
Vector2 powerup = {4, 5};
Vector2 result1 = position + speed * powerup;
Vector2 result2 = (position + speed) * powerup;
if (result1 == result2)
{
// ...
}
std::cout << result1 << std::endl;
std::cout << result2 << std::endl;
// operator overloading is very useful for smart pointers (see smart-pointers.cpp)
ScopedPtr vector = new Vector2{1, 2};
std::cout << vector->x << std::endl;
// without operator overloading, this would be something like "vector.GetObject()->x"
}