Why is it called operator overloading?

后端 未结 2 1727
挽巷
挽巷 2021-01-21 07:26

If the following class, Foo, is defined. It is said it overloads the unary ampersand (&) operator:

class Fo         


        
2条回答
  •  无人共我
    2021-01-21 08:15

    Consider the following code:

    int x;
    Foo y;
    &x; // built-in functionality
    &y; // y.operator&();
    

    We have two variables of different types. We apply the same & operator to both of them. For x it uses the built-in address-of operator whereas for y it calls your user-defined function.

    That's exactly what you're describing as overloading: There are multiple functions (well, one of them is the built-in functionality, not really a "function") and they're selected based on the type of the operand.

提交回复
热议问题