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 ;)
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.
ASKER
Thanks a lot jkr.
ASKER