当前位置:网站首页>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边栏推荐
- Leetcode 2185. Counts the string containing the given prefix
- Briefly describe the difference between CGI and fastcgi
- 阿里云开发板vscode开发环境搭建
- After reading the question, you will point to offer 16 Integer power of numeric value
- What is automatic bidding? What are its advantages?
- CUDA error: CUBLAS_ STATUS_ NOT_ INITIALIZED when calling `cublasCreate(handle)`
- Alicloud development board vscode development environment setup
- 通过loganalyzer展示数据库中的日志
- Codeforces Round #798 (Div. 2)(A~D)
- 高考回忆录
猜你喜欢

【活动早知道】LiveVideoStack近期活动一览

chrome://tracing Performance analysis artifact

阿裏雲開發板HaaS510報送設備屬性

动态搜索广告智能查找匹配关键字

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

Tlm/systemc: TLM socket binding problem
![[advanced MySQL] evolution of MySQL index data structure (IV)](/img/eb/e32387b172eb4c3a4152dbc3b0cb8f.png)
[advanced MySQL] evolution of MySQL index data structure (IV)

编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构

SystemC common errors

Briefly describe the difference between CGI and fastcgi
随机推荐
公司运营中更注重转化的出价策略,如何实现? —Google sem
Des File Encryptor based on MFC framework
Player actual combat 13 create qtopengl project to promote window control and reload qoopenglwedge
阿里云开发板HaaS510解析串口JSON数据并发送属性
初学者入门阿里云haas510开板式DTU(2.0版本)--510-AS
什么是自动出价?它的优势是什么?
Create a small root heap and judge the node relationship (also.C\u str() substr(),atoi(),string. Use of find())
高考回憶錄
Leetcode 2185. Counts the string containing the given prefix
Implementation and debug of process hiding under x64
【mysql进阶】索引分类及索引优化方案(五)
完美收官|详解 Go 分布式链路追踪实现原理
How to use Android studio to create an Alibaba cloud Internet of things app
Crack WinRAR to ad pop-up window
After reading the question, you will point to offer 16 Integer power of numeric value
Use of pytorch (to be supplemented)
正点原子STM32F429核心板的插座型号
Mold and remainder
Write policy of cache
阿里云开发板HaaS510连接物联网平台--HaaS征文