Hey ! I was bored and I had this idea so here it is. It's a simple class containing a Data field and a Min and Max field where Data will be a number between Min and Max. Why is this in game programming ? Well, you can use this to contain the health, mana or whatever you wish. I've overloaded most operators though I can't see to make the pre and post-increment operators work. A problem of syntax, perhaps someone can help.

PS: I know it may be a little bit of overhead but I think it cleans up a little of the mess in your code since you don't have to check boundaries and such.

Code:#ifndef RANGED_NUM_H_INCLUDED #define RANGED_NUM_H_INCLUDED #define READJUST_VALUE(a, min, max) if(a < min) a = min; if(a > max) a = max template <typename T> class RangedNum { public: explicit RangedNum(T dat = 0, T min = 0, T max = 0) : Data(dat), Min(min), Max(max) { } explicit RangedNum(const RangedNum<T>& cpy) { Data = cpy.Data; Min = cpy.Min; Max = cpy.Max; } void SetMinimum(T min = 0) { Min = min; if(Min > Max) Min = Max; READJUST_VALUE(Data, Min, Max); } void SetMaximum(T max = 0) { Max = max; if(Max < Min) Max = Min; READJUST_VALUE(Data, Min, Max); } void SetRanges(T min, T max) { Min = min; Max = max; if(Min > Max) Min = Max; READJUST_VALUE(Data, Min, Max); } operator T () { return Data; } template <typename OpT> T& operator + (const OpT& arg) { Tmp = Data + Arg; READJUST_VALUE(Tmp, Min, Max); return Tmp; } template <typename OpT> T& operator += (const OpT& arg) { Data += arg; READJUST_VALUE(Data, Min, Max); return Data; } template <typename OpT> T& operator - (const OpT& arg) { Tmp = Data - Arg; READJUST_VALUE(Tmp, Min, Max); return Tmp; } template <typename OpT> T& operator -= (const OpT& arg) { Data -= arg; READJUST_VALUE(Data, Min, Max); return Data; } template <typename OpT> T& operator * (const OpT& arg) { Tmp = Data * Arg; READJUST_VALUE(Tmp, Min, Max); return Tmp; } template <typename OpT> T& operator *= (const OpT& arg) { Data *= arg; READJUST_VALUE(Data, Min, Max); return Data; } template <typename OpT> T& operator / (const OpT& arg) { if(arg = 0) arg = 1; Tmp = Data / Arg; READJUST_VALUE(Tmp, Min, Max); return Tmp; } template <typename OpT> T& operator /= (const OpT& arg) { if(arg = 0) arg = 1; Data /= arg; READJUST_VALUE(Data, Min, Max); return Data; } T& operator ++(RangedNum<T>& arg) { arg.Data++; return arg.Data; } T& operator ++ () { Data++; return Data; } template <typename OpT> bool operator == (const OpT& arg) const { return (Data == arg); } template <typename OpT> bool operator != (const OpT& arg) const { return (Data != arg); } protected: T Data, Min, Max, Tmp; }; #endif // RANGED_NUM_H_INCLUDED