Item 26. Operator Function Lookup

xiaoxiao2021-03-05  23

Item 26. Operator Function Lookup

Sometimes Operator Function looks like a member operator function Overload a non-member operator, but it is not the case. This is not OverLoading, just a different lookup algorithm. Class x {public: x operator% (const x "const; // binary modulus x memfunc1 (const x &); void memfunc2 (); // ...};

X a, b, c; a = b% C; // infix call to member operator% a = b.operator% (c); // Member function calla = B.MEMFUNC1 (C); // Another Member Function Call

When using the MEMBER FUNCTION CALL, lookup rules follow the Member Function Lookup, which is different, eg: x operator% (const X :: "Void x ::" // Non-Member Operator // ... void x :: Memfunc2 () {* this% 12; // Calls Non-Member Operator% Operator% (* this, 12); // error! Parameter number does not match, because the call is x :: operator% (const x &) } It will take into account the Infix Operator Call, member functions, and non-member function compilers. Therefore, when the Operator% is subjected to Infix Call, Non-Member Operator will be found. This is not OverLoading, just a different lookup algorithm. When calling the Non-Infix Call function Operator% (* this, 12), follow the standard function lookup rules, and find the member function, and the number of incorrect parameters has occurred.

For efficiency, INFIX Calls uses a degraded ADL: When determining which function of OverLoad, only consider the left parameter domain and full-local domain of Infix Operator. The ADL includes the Namespaces domain introduced by the parameters.

转载请注明原文地址:https://www.9cbs.com/read-35704.html

New Post(0)