Link to home
Create AccountLog in
Avatar of perlperl
perlperl

asked on

operator overload

I am confused how C++ calls this function internally

class MyClass {

   public:
      int num;
      MyClass(int val) : num(val) {}
};
MyClass& decode_32(uint32_t& in, MyClass& r);

MyClass& operator >> (MyClass& s, uint32_t& v)
{
        return decode_32(v, s);
}

MyClass& decode_32(uint32_t& in, MyClass& r) {
    in = r.num;
    return r;
}

int main(int argc, char *argv[]){
   MyClass obj(10);
   uint32_t val;
   obj >> val; // HOW Does this call overloaded ">>" operator
   printf ("Val = %d\n", val);

}

It is so difficult to read this ;) 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of perlperl
perlperl

ASKER

Is this special behaviour for overloaded operator?
Kinda specialm yes, ad well as the requirement for the overloaded operator to return a reference to the left-hand-side object they are working on. This is needed to be able to 'chain' such calls, as the language explicitly allows that.
Thanks a lot jkr.