当前位置:网站首页>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边栏推荐
- Implementation of Ackermann function with simulated recursion
- Alibaba cloud development board haas510 responds to UART serial port instructions
- 阿里云开发板HaaS510报送设备属性
- 注重点击,追求更多用户进入网站,可以选择什么出价策略?
- chrome://tracing Performance analysis artifact
- How to use Android studio to create an Alibaba cloud Internet of things app
- WinDbg preview debug analysis createtoolhelp32snapshot
- 【活动早知道】LiveVideoStack近期活动一览
- 阿里云开发板vscode开发环境搭建
- Mémoire de l'examen d'entrée à l'université
猜你喜欢

Tlm/systemc: TLM socket binding problem

Notepad common settings

Llvm pass-- virtual function protection

拆改广告机---业余解压

阿里云开发板HaaS510报送设备属性

什么是自动出价?它的优势是什么?

Player actual combat 16 xdecode class

Compile and install lamp architecture of WordPress and discuz for multi virtual hosts based on fastcgi mode

How to use Android studio to create an Alibaba cloud Internet of things app

初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS
随机推荐
Player practice 17 xvideowidget
[early knowledge of activities] list of recent activities of livevideostack
Factory mode of "object creation" mode
Design of PLC intelligent slave station based on PROFIBUS DP protocol
Analysis of lua source code
M1 pod install pod lint failure solution
After reading the question, you will point to offer 16 Integer power of numeric value
Chapter IV expression
[advanced MySQL] evolution of MySQL index data structure (IV)
Leetcode 2176. Count equal and divisible pairs in an array
Remote code injection
To SystemC Beginners: the first program
Shell notes
Postgresql14 installation and use tutorial
浅谈中国程序员为什么要跳槽?
[semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX
Player actual combat 16 xdecode class
Implementation and debug of process hiding under x64
[semidrive source code analysis] [x9 chip startup process] 26 - LK of R5 safetyos_ INIT_ LEVEL_ Target phase code flow analysis (TP drvier, audio server initialization)
1414. minimum number of Fibonacci numbers with sum K