...

/

Implementing the LengthProxy Object

Implementing the LengthProxy Object

Learn how to implement the LengthProxy object for comparing and calculating lengths while preventing misuse.

LengthProxy object

It’s time to implement the LengthProxy class containing a float data member that represents the squared length. The actual squared length is never exposed to prevent users of the class from mixing the squared length with the regular length.

Instead, LengthProxy has a hidden friend function that compares its squared length with a regular length, as follows:

Press + to interact
class LengthProxy {
public:
LengthProxy(float x, float y) : squared_{x * x + y * y} {}
bool operator==(const LengthProxy& other) const = default;
auto operator<=>(const LengthProxy& other) const = default;
friend auto operator<=>(const LengthProxy& proxy, float len) {
return proxy.squared_ <=> len*len; // C++20
}
operator float() const { // Allow implicit cast to float
return std::sqrt(squared_);
}
private:
float squared_{};
};

We have defined the operator float() to allow implicit casts from LengthProxy to float. LengthProxy objects can also be compared with each other. By using the new C++20 comparisons, we simply default the equality operator and three-way comparison operator to have the compiler generate all the necessary comparison operators for us.

Next, we rewrite the Vec2D class to return objects of the class LengthProxy instead of the actual float length:

Press + to interact
class Vec2D {
public:
Vec2D(float x, float y) : x_{x}, y_{y} {}
auto length() const {
return LengthProxy{x_, y_}; // Return proxy object
}
float x_{};
float y_{};
};

With these additions, it’s time to use our new proxy class.

Comparing lengths with LengthProxy

In this example, we’ll compare two vectors, a and b, and determine whether ...