Implementing the DistProxy object

Now, let's create the proxy object for distances between points: DistProxy. A point distance simply consists of the squared distance between two points and a comparison to a distance. The actual squared distance is never exposed in order to prevent the library user from mixing the squared distance with the regular distance. That is, if we do not take precaution, the user of the code might accidentally write the following:

auto a = Point{23, 45}; 
auto b = Point{55, 66}; 
auto spot = Point{34, 55}; 
// Subtle bug below as distance is compared to distance_squared 
bool a_is_nearest = a.distance(spot) < b.distance_squared(spot); 

Thus, he may end up accidentally comparing the squared distance with the real distance, ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.