C ++ overloaded functions

1. What is overloaded functions

  With a different function name defined function, when the same function name, different parameters with different meanings.

  E.g:

  

#include <stdio.h>
#include <string.h>


int func(int x)
{
    return x;
}

int func(int x,int y)
{
    return x + y;
}

int func(const char *s)
{
    return strlen(s);
}



int main(int argc, char *argv[])
{
    printf("Begin...\n");
    printf("%d\n" ,func(1));
    printf("%d\n" ,func(2,3));
    printf("%d\n" ,func("abcdefg"));
    printf("End...\n");
    return 0;
}

  operation result:

Begin...
1
5
7
End...

 

  The above code is the same function name in the C language is certainly an error because the C language does not support overloaded functions. In C ++, it is not being given. And the corresponding function is invoked according to the parameters.

 

2. What are the conditions to allow overloaded?

  2.1, different parameter types

  2.2, the number of different parameters

  2.3, different parameters order

  Wherein three conditions are satisfied to meet a heavy load.

  Example: The following code will satisfy the first two func order different parameters 2.3  

#include <stdio.h>
#include <string.h>

int func(const char *s ,int a)
{
    return a;
}

int func(int a,const char *s )
{
    return strlen(s);
}


int main(int argc, char *argv[])
{
    printf("Begin...\n");
    printf("%d\n",func("abc",2));
    printf("%d\n",func(2,"abc"));
        printf("End...\n");
        return 0;
}

  operation result:

Begin...
2
3
End...

 

3, the compiler calls the overloaded criteria:

  3.1, all the functions with the same name as a candidate

  3.2, try to find a viable candidate parameters

    3.2.1, exact match argument

    3.2.2 with the default parameters can be matched argument

    3.2.3, by default type conversion match argument

Matching reason for the failure:

  1, to find out the final candidate is not the only function, ambiguity occurs, the compilation fails.

  For example: Here call printf ( "% d \ n" func (1,2)); when there are two conditions can be matched so the compiler does not know that function should be called.

#include <stdio.h>
#include <string.h>

int func(int a,int b)
{
    return a + b;
}

int func(int a,int b,int c=10)
{
    return a+b+c;
}


int main(int argc, char *argv[])
{
    printf("Begin...\n");
    printf("%d\n" func(1,2));
        printf("End...\n");
        return 0;
}

 Compile the results: 

test.cpp:18:25: error: call of overloaded ‘func(int, int)’ is ambiguous
  printf("%d\n" ,func(1,2));
                         ^
test.cpp:4:5: note: candidate: int func(int, int)
 int func(int a,int b)
     ^~~~
test.cpp:9:5: note: candidate: int func(int, int, int)
 int func(int a,int b,int c=10)
     ^~~~

 

  2, can not match all the candidates, the function is not defined, the compilation fails

4, function overloading precautions.

  4.1, overloaded functions in essentially independent function

  4.2, functions of different types of overloaded functions

  4.3, the function returns a value basis as a function of not overloaded

 Function overloading is determined by the function name and parameter list.    

Guess you like

Origin www.cnblogs.com/hjxzjp/p/11595106.html