当前位置:网站首页>Introduction to functions (inline functions and function overloading)

Introduction to functions (inline functions and function overloading)

2022-06-12 14:13:00 Panda general

1. Inline function

2. function overloading

Overload concept : The same identifier has different meanings in different contexts ;
function overloading :
– The same function name defines different functions ;
– When the function name is matched with different parameters , The meaning of the expression is also different ;
– Determined by function name and function parameter list , It has nothing to do with the return value ;
Function overloading must meet at least the following conditions :
– The number of parameters is different
– Different parameter types
– Parameter order is different
Principles for compiler calling function overloading :
① Take all functions with the same name as candidates
② Try to find possible candidate functions ( The following are carried out simultaneously )
– Find exactly by function name ;

#include <iostream>
int func(int a, int b)
{
    
     return a + b;
}

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

int main()
{
    
    func(1,2);            // Find exactly according to the function name 
    return 0;
}

– The arguments can be matched by default parameters ;

#include <iostream>
int func(int a, int b, int c)
{
    
     return a + b;
}
int func(int a, int b)
{
    
     return a + b; 
}

int main()
{
    
    func(1,2);            // After the first two parameters match , The compiler will also try to add a default parameter to see if there is a match 
    return 0;             // term , For example, in this example, the matching function has default parameters c, The compilation fails , Secondary definition 
}

– You can convert arguments by default types ;

#include <iostream>
int func(int a, int b, int c)
{
    
     return a + b;
}
int func(int a, int b)
{
    
     return a + b;
}
int main()
{
    
    func('a',2);            // After the compiler matches the non character type parameter , Will try to convert the character type to int Type matching 
    return 0;              // The rest of the functions ; 
}

③ Matching failure
– The feasible candidate function finally found is not unique , It has two meanings ;
– All candidate parameters do not match , Function undefined ;
Function overloading and function pointer
– When a function pointer is assigned with an overloaded function name
– Select the candidate consistent with the function pointer parameter list according to the overload rules ;
– Strictly match the function type of the candidate with the function type of the function pointer ;

#include <iostream>
int func(int a)
{
        
    return a;
}
int func(int a, int b)
{
    
    return a + b;
}
int func(const char *a)
{
    
    return strlen(a);
}
typedef int(*Punc)(int a);

int main()
{
    
   Punc p = func;
   int c = p(1);      // call func(int a)
   printf("c = %d\n", c);
   return 0;    
}

3.C And C++ Mutual invocation

C++ Compiler takes precedence C++ How to compile , utilize extern Keywords force C++ For compiler C The way to compile ;

 Format :extern "C"
{
     

}

#ifdef __cplusplus //C++ Built in macros , As long as it is C++ The compilation method has this macro , Realization C and 
extern "C"                     //C++ Can compile this file 
{
    
} 
#endif
原网站

版权声明
本文为[Panda general]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010513238886.html