1

I would like to override all = operators where the lhs is a known type the the rhs is my own class. As an example:

    class MyClass;
    class Override {
        long operator+=(long X, const MyClass& Y);
    }
    long Override::operator+=(long X, const MyClass& Y) {
       return X += (long)Y;
    }
    void main(int argc, char** argv) {
       MyClass X(1);
       long Y = 1;
       Y += 1;        // works great
       Y += (long)X;  // works great
       Y += X;        // does not work
    }

And MyClass has the appropriate casting and creation methods.

I know I'm missing something but I can't figure out what.

I get a compiler error on

    Y += X

with the following function

    long Override::operator(long& X, const MyClass& Y) ...

Can anyone tell me what the correct way of doing this is?

thanks art

lostbits
  • 928
  • 1
  • 9
  • 23

2 Answers2

3

The class Override is totally useless here. Just write a global function:

long &operator+=(long &X, const MyClass& Y) {
   return X += (long)Y;
}

Note that the first parameter (X) must be passed by reference, as it will be modified by the operator.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • 1
    `+=` should return by reference. – juanchopanza Feb 04 '13 at 22:56
  • 1
    @juanchopanza: Oh, you are right, the result of an assignment in C++ (unlike in C) is an l-value! Corrected. – rodrigo Feb 04 '13 at 23:12
  • The real MyClass is an ADT where the ++ operation is illegal in some of the derived classes. Although (long)Y will report an error, the error will be because of an illegitimate cast rather and an illegal operation. But if I gotta' do it, then I gotta' do it. However when I tried "long operator+=(long& X, const MyClass& Y)" I received an error "undefined reference to `slip::operator+=(unsigned char, slip::SlipDatum const&)" (and the code here uses unsigned char & signed char & long & unsigned long ...). I don't know how to get rid of the error? – lostbits Feb 04 '13 at 23:14
  • I miswrote the Override class. You are right and the actual code does have a function with two parameters as you have suggested, except, using "long &X" produces an error message for "Y += X" saying that the function for "operator+=(long X, const MyClass& X)" can not be found. I have no clue how to fix the error. – lostbits Feb 04 '13 at 23:31
  • @ArthurSchwarez: Well, if you miscopied the code... then you should copy the real code again, or any help you get will be misguided. Anyway... my example compiles fine, are you sure that your `operator+=` is defined as a global function and not a member of some class? – rodrigo Feb 04 '13 at 23:59
0

Like this (I created some MyClass castable to long just for example)

class MyClass
{
    long a;
    public:
    MyClass(long a) : a(a) {}
    operator long () const
    {
        return a;
    }
};

long operator+=(long X, const MyClass& Y) {
    return X += (long)Y;
}
int main(int argc, char** argv) {
    MyClass X(1);
    long Y = 1;
    Y += 1;        // works great
    Y += (long)X;  // works great
    Y += X;        // works too
    return 0;
}
peper0
  • 3,111
  • 23
  • 35
  • Again, please avoid implicit conversions. They bite. – gimpf Feb 04 '13 at 22:53
  • Your "long oerator(long X, const MyClass& Y)" doesn't have "long& X". A previous poster said that a reference is needed Does this matter? – lostbits Feb 04 '13 at 23:24
  • Thank you. Through a succession of extremely dumb coding errors I created a monster. You and others have solved my problem. – lostbits Feb 04 '13 at 23:42
  • Right, += should return a reference. It's not a must, but have some advantages. See http://stackoverflow.com/questions/3105798/why-must-the-copy-assignment-operator-return-a-reference-const-reference – peper0 Feb 05 '13 at 09:20