Is there a way to overload operator/()
to use the class as the denominator?
Like so:
int foo = 5 / object;
Is there a way to overload operator/()
to use the class as the denominator?
Like so:
int foo = 5 / object;
Use a free function:
int operator/ (const int, const MyClass &);
If it needs to access private members that you have no interface for, make it a friend inside your class as well:
friend int operator/ (const int, const MyClass &);
Use a free function instead of a member function for operator/
.
Binary operators typically have the same operand types. Assuming that foo
has a non-explicit constructor taking an int
, you would have:
struct foo
{
foo(int i) {};
};
int operator/(foo const& x, foo const& y);
Free function is your friend:
int operator/ (int, const MyClass &);