Multiple operator override behaviors by manipulating type

Viewed 52

So let's say I'm overwriting the inequality checks of a structure describing a point. I have it set to compare the magnitude by default.

I want to have an option to check each value instead, and I was thinking of ways to do it inline. I could make a function that recasts it as a dummy structure so it has a different type when comparing.

struct vec3 {
    double x,y,z;
    struct _all {double x,y,z};
    friend _all All (vec3 &A) { return *(_all*) &vec3;}
    struct _any {double x,y,z};
    friend _any Any (vec3 &A) { return *(_any*) &vec3;}
    friend bool operator < (vec3 &A, double B) { /* does the magnitude check */ }
    friend bool operator < (_all &A, double B) { return A.x<B && A.y < B && A.z<B; }

    friend bool operator < (_any &A, double B) { return A.x<B || A.y < B || A.z<B; }
};

This is all so I can write

if (All(PointA) < Limit) {}

I have a feeling this breaks a bunch of coding practices, but I'm having a hard time finding any similar problem. Maybe it's safer to use a constructor instead of type changes? Or would it be clearer if I wrote a function IsAnyLessThan(double A) for my structure instead?

0 Answers
Related