Is it possible to overload operator% for two doubles?
const double operator%(const double& lhs, const double& rhs) { return fmod(lhs, rhs); }
Of course, this generates an error, because one of the two parameters must have a class type. So I thought about using the C ++ constructor implicit invocation feature to get around this problem. I did it as follows:
class MyDouble { public: MyDouble(double val) : val_(val) {} ~MyDouble() {} double val() const { return val_; } private: double val_; }; const double operator%(const MyDouble& lhs, const double& rhs) { return fmod(lhs.val(), rhs); } const double operator%(const double& lhs, const MyDouble& rhs) { return fmod(lhs, rhs.val()); }
... and:
double a = 15.3; double b = 6.7; double res = a % b;
Unfortunately this will not work! Any hints, ideas, ... appreciated! Thanks in advance, Jonas
Jonas source share