C++ - Overload

  1. Introduction

    About method members, not attribute members.

    Term

    • Overloading: two or more methods in one class have the same method name but different parameters.
    • Overriding: two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class.

    Declaration and defintion

    C++ compiler thinks the following are different declarations

    void func(int x){}
    void func(float x){}
    void func(double x){}
    void func(int & x){}
    class F{};
    class G: public F{};
    void func(F x){}
    void func(G x){}
    
    

    C++ compiler does not allow the following overloading

    void func(int x){
    
    }
    int func(int x){
    
    }
    // functions that differ only in their return type cannot be overloaded
    

    Calling

    C++ compiler interrupts number value with following equal preference. Therefore it can cause calling ambiguous.

    1. double -> double or float
    2. float -> double or float
    3. int -> int or double or float
    4. Moreover, compiler cannot distinguish call by reference or call by value.

    C++ compiler prefers to use most close class type to interrupt class value.

    What can be distinguished: #parameter, order of parameter (but double -> float, float -> double, int -> float, int -> double)

    What can not be distinguished: reference or not. e.g. func(int x); and func(int &x);

  2. Overload Examples

    1. Order is matter

      class Overload{
          void add(int x, float b);
          void add(float b, int x);
      };
      int main(){
          ld.add(1.5, 1); // the second add called.
          ld.add(1 , 1.4); // the first add called.
      }
      
    2. But float and double are same in function signature. e.g. add(double, float) and add(float, double) cause ambiguous

      Overload by using different number of arguments is easy. The diffcult part is same number of arguments but different orders. Especially if all of those argument are number types.

      class Overload{
          void add(float b, double c);
          void add(double c, float b);
      };
      // overload.cpp:48:8: error: call to member function 'add' is ambiguous
      
    3. Moreover, the definition sometime is not ambiguous, but calling can cause ambiguous.

      class Overload{
      public:    
          void add(float b, double c);
          //void add(double c, float b);
          void add(int x, float b);
          void add(float b, int x);
      };
      
      int main(){
          Overload ld;
          ld.add(1 , 1.4);
          return 0;
      }
      

      Without the main call ld.add(1, 1.4), compilation is successful. But this call causes

      overload.cpp:34:8: error: call to member function 'add' is ambiguous
          ld.add(1 , 1.4);
          ~~~^~~
      overload.cpp:17:16: note: candidate function
      void Overload::add(float b, double c){
                     ^
      overload.cpp:24:16: note: candidate function
      void Overload::add(int x, float b){
                     ^
      overload.cpp:27:16: note: candidate function
      void Overload::add(float b, int x){
          

    Just remember

    1. Float and Double are same in the function overload becuase compile cannot tell 1.7 is a float or double.
    2. During calling, int can be interpreted as a int, float, or double, preference are same.
    3. But Compiler prefer to interpret double/float as double/float, if not found corresponding function signature, compile treat them as int.

  3. Introduction

    Operator in C++ is just like a special function, the operands around a operator is just its arguments; the value it returns is the return value.

    Operator can be treated as a class member, or just a global function. But there is some convention for their usage.

    Depending on operator, it may takes 1 argument or 2 argument.

    operator#arguments