PROPOSAL: Operator overloading by static member function

oldrev oldrev at gmail.com
Sun Oct 14 05:34:59 PDT 2007


Janice Caron Wrote:

> On 10/13/07, oldrev <oldrev at gmail.com> wrote:
> > It's a simple way to avoid using the ugly "opAdd_r", eg:
> >
> > struct Currency
> > {
> >   private int m_value;
> >
> >   public static Currency opAdd(Currency lhs, Currency rhs) {
> >     return Currency(lhs.m_value + rhs.m_value);
> >   }
> > }
> >
> 
> Returning to the original thread topic, how should the compiler react
> if I "cheat" and do
> 
> struct Currency
> {
>   private int m_value;
> 
>   public static Currency opAdd(int lhs, int rhs) {
>     return Currency(lhs + rhs);
>   }
> }
> 
> I'm sure you can see the problem here. I've just redefined int + int.
> Surely, that can't be allowed?

Noway, when you overloading a binary operator, it must has a Currency parameter at least.

Regards,
- oldrev



More information about the Digitalmars-d mailing list