Analysis of function overloading

Overload (Overload)
the same identifier have different meanings in different contexts
such as:
  "wash" has a different meaning and different words with the
  laundry, wash, brainwashing, washing the toilet
  "play" with different words and after have different meanings
   play chess, play piano, play basketball

overloaded in natural language is everywhere, so if programming is also reload it?

Function overloading in C ++
function overloading (Function Overload)
- defines different functions with the same name of a function
- when the function name and the different parameters of the function with different meanings
int FUNC (int X)
{
  return X;
}

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

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


Conflict 1: default function arguments function overloading encounter what happens when?
FUNC int ( int A, int B, int C = 0 )
{
  return C * A * B;
}

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

int main()
{
int c = func(1,2)
  return 0;
}

Guidelines compiler calls overloaded functions
- all functions with the same name as a candidate
- to try to find a viable candidate functions
exactly match the parameters
by default parameters can match the argument
by default match type conversion argument

fails to match
eventually find a candidate function is not unique , ambiguity appeared, fails to compile
can not match all the candidates, the function is not defined, the compilation fails

Note overloaded functions
- overloaded function in nature are separate different functions
- overloaded function of the function of different types
- function returns a value basis as a function of not overloaded

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

int the Add ( int A, int B) // function of type int (int, int) 
{
     return A + B;
}

int add(int a, int b, int c) //函数的类型为int(int,int,int)
{
    return a+b+c;
}

int main()
{
    printf("%p\n",(int(*)(int,int))add);
    printf("%p\n",((int*)(int,int,int)add));
}

小结:
函数重载是C++中引入的概念
函数重载用于模拟自然语言中的词汇搭配
函数重载使得C++具有更丰富的语义表达能力
函数重载的本质为相互独立的不同函数
C++中通过函数名和函数参数确定函数调用

Guess you like

Origin www.cnblogs.com/-glb/p/11886461.html