-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_nonabstract.cpp
More file actions
42 lines (39 loc) · 1.29 KB
/
Copy pathtest_nonabstract.cpp
File metadata and controls
42 lines (39 loc) · 1.29 KB
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
#include <cmath>
class IVec3 {
public:
virtual ~IVec3() = default;
virtual float x() const noexcept { return 0; }
virtual float y() const noexcept { return 0; }
virtual float z() const noexcept { return 0; }
virtual void setX(float) {}
virtual void setY(float) {}
virtual void setZ(float) {}
virtual IVec3 add(const IVec3& rhs) const {
return IVec3(x() + rhs.x(), y() + rhs.y(), z() + rhs.z());
}
protected:
IVec3() = default;
IVec3(float, float, float) {} // dummy for derived class convenience
};
class Vec3 final : public IVec3 {
float m_x, m_y, m_z;
public:
Vec3() : m_x(0), m_y(0), m_z(0) {}
Vec3(float x, float y, float z) : m_x(x), m_y(y), m_z(z) {}
float x() const noexcept override { return m_x; }
float y() const noexcept override { return m_y; }
float z() const noexcept override { return m_z; }
void setX(float v) override { m_x = v; }
void setY(float v) override { m_y = v; }
void setZ(float v) override { m_z = v; }
Vec3 add(const IVec3& rhs) const override {
return Vec3(m_x + rhs.x(), m_y + rhs.y(), m_z + rhs.z());
}
};
int main() {
Vec3 a(1,2,3), b(4,5,6);
Vec3 c = a.add(b);
IVec3* p = &a;
IVec3 d = p->add(b); // Returns IVec3 by value through interface pointer
return 0;
}