当前位置: 首页 > news >正文

青园网站建设南京百度推广

青园网站建设,南京百度推广,静安做网站公司,看室内设计案例的网站目录 1.引例 2.函数重载的概念 3.C支持函数重载的原理 1.引例 倘若现在要实现一个加法计算器,用C语言实现的话我们会选择这样的方式: int Add_int(int a, int b) {return a b; }double Add_double(double a, double b) {return a b; } 在使用加…

目录

1.引例

2.函数重载的概念

3.C++支持函数重载的原理 


1.引例

倘若现在要实现一个加法计算器,用C语言实现的话我们会选择这样的方式:

int Add_int(int a, int b)
{return a + b;
}double Add_double(double a, double b)
{return a + b;
}

在使用加法计算器时,需要根据需求调用不同的函数:

int main()
{cout << Add_int(10, 20) << endl;cout << Add_double(2.2, 3.3) << endl;return 0;
}

这里存在一个让人不舒服的点,明明都是简单的加法操作,却因为参数不同的原因在调用函数时需要指定函数名。凭什么它们两个函数不能都叫作Add呢?

针对这个问题,C++中提出了函数重载的概念。函数定义时可以重名,调用函数时,编译器会根据所传参数的类型自动匹配相应的函数

例如,有了函数重载后,加法计算器就可以这样设计:

int Add(int a, int b)
{return a + b;
}double Add(double a, double b)
{return a + b;
}int main()
{//调用函数cout << Add_int(10, 20) << endl;cout << Add_double(2.2, 3.3) << endl;return 0;
}

2.函数重载的概念

函数重载:是函数的一种特殊情况,C++允许在同一作用域中声明几个功能类似的同名函数,这些同名函数的形参列表(参数个数类型类型顺序)不同,常用来处理实现功能类似数据类型不同的问题。例如:

#include<iostream>
using namespace std;//1.参数类型不同
int Add(int a, int b)
{return a + b;
}double Add(double a, double b)
{return a + b;
}//2.参数个数不同
int Add(int a, int b)
{return a + b;
}int Add(int a, int b, int c)
{return a + b + c;
}//3.参数类型的顺序不同void F(int a, char b)
{//...
}void F(char a, int b)
{//...
}int main()
{Add(10, 20);Add(10, 20, 30);Add(2.2, 3.3);F(10, 'a');F('a', 10);return 0;
}

3.C++支持函数重载的原理 

我们用函数重载定义函数Add,在我们眼中,两个函数名字相同,参数不同,调用函数时,我们知道应该调用哪个,那么编译器在链接阶段,如何知道去哪寻找对应的函数栈帧呢?

int Add(int a, int b)
{return a + b;
}double Add(double a, double b)
{return a + b;
}int main()
{Add(10, 20);Add(2.2, 3.3);return 0;
}

其实编译器在编译与汇编阶段,会对函数名做修饰。不同的编译器对函数名的修饰也不同,为了方便观察,这里以Linux环境下的g++编译器为例。输入指令查看可执行程序反汇编的代码:

g++ Test.cpp -o Testcpp
objdump -S Testcpp

以下是Linux下g++修饰的汇编代码:

为了作对比,我们再看看没有函数重载的C语言是否会对函数名做修饰:

gcc Test.c -o Testc
objdump -S Testc

C代码:

#include<stdio.h>int Add_int(int a, int b)
{return a + b;
}double Add_double(double a, double b)
{return a + b;
}int main()
{Add_int(10, 20);Add_double(2.2, 3.3);return 0;
}

 汇编代码:

通过这里就理解了C语言没办法支持重载,因为同名函数没办法区分。而C++是通过函数修饰规则来区分,只要参数不同,修饰出来的名字就不一样,就支持了重载。 

最后补充,如果两个函数函数名和参数是一样的,返回值不同是不构成重载的,因为调用时编译器没办法区分。

 


文章转载自:
http://dinnconociassociation.ssfq.cn
http://dinncorewardless.ssfq.cn
http://dinncochamaephyte.ssfq.cn
http://dinncocervantite.ssfq.cn
http://dinncomiogeosyncline.ssfq.cn
http://dinncoligneous.ssfq.cn
http://dinncoaconitase.ssfq.cn
http://dinncobellerophon.ssfq.cn
http://dinncoholohedry.ssfq.cn
http://dinncomeganewton.ssfq.cn
http://dinncoplacket.ssfq.cn
http://dinncohackberry.ssfq.cn
http://dinncoindivisible.ssfq.cn
http://dinncofirewarden.ssfq.cn
http://dinncotrunkfish.ssfq.cn
http://dinncodoored.ssfq.cn
http://dinncoesu.ssfq.cn
http://dinncoperlustrate.ssfq.cn
http://dinncotisza.ssfq.cn
http://dinncococcidia.ssfq.cn
http://dinncogetparms.ssfq.cn
http://dinncowindbag.ssfq.cn
http://dinncotailband.ssfq.cn
http://dinncofenderless.ssfq.cn
http://dinncodextropropoxyphene.ssfq.cn
http://dinncoconcrete.ssfq.cn
http://dinncorhetor.ssfq.cn
http://dinncolyase.ssfq.cn
http://dinncovictimless.ssfq.cn
http://dinncodearie.ssfq.cn
http://dinncoruttish.ssfq.cn
http://dinncosecession.ssfq.cn
http://dinncoking.ssfq.cn
http://dinncokaryosystematics.ssfq.cn
http://dinncotraveling.ssfq.cn
http://dinncocrosspatch.ssfq.cn
http://dinncocalibrater.ssfq.cn
http://dinncouprush.ssfq.cn
http://dinncoungulae.ssfq.cn
http://dinncoknifepoint.ssfq.cn
http://dinncofaunistic.ssfq.cn
http://dinncoglitter.ssfq.cn
http://dinncocommuterdom.ssfq.cn
http://dinncospga.ssfq.cn
http://dinncosonet.ssfq.cn
http://dinncotreadle.ssfq.cn
http://dinncoundershrub.ssfq.cn
http://dinncoanthropologic.ssfq.cn
http://dinncocosine.ssfq.cn
http://dinncotreponeme.ssfq.cn
http://dinncolunik.ssfq.cn
http://dinncobutyric.ssfq.cn
http://dinnconekulturny.ssfq.cn
http://dinncoquicken.ssfq.cn
http://dinncogodless.ssfq.cn
http://dinncorepp.ssfq.cn
http://dinncotorpify.ssfq.cn
http://dinncoconnubial.ssfq.cn
http://dinncocloyless.ssfq.cn
http://dinncoaleksandrovsk.ssfq.cn
http://dinncobiocytin.ssfq.cn
http://dinncospectacular.ssfq.cn
http://dinncoexpatriate.ssfq.cn
http://dinncofie.ssfq.cn
http://dinncopresident.ssfq.cn
http://dinncolacedaemonian.ssfq.cn
http://dinncobashlyk.ssfq.cn
http://dinncotriskaidekaphobe.ssfq.cn
http://dinncocomplot.ssfq.cn
http://dinncomadhouse.ssfq.cn
http://dinncoprocambium.ssfq.cn
http://dinnconowt.ssfq.cn
http://dinncodenude.ssfq.cn
http://dinnconotchery.ssfq.cn
http://dinncokano.ssfq.cn
http://dinncophenocopy.ssfq.cn
http://dinncorimy.ssfq.cn
http://dinncostrychnos.ssfq.cn
http://dinncospontoon.ssfq.cn
http://dinncopute.ssfq.cn
http://dinnconinety.ssfq.cn
http://dinncofeatheredged.ssfq.cn
http://dinncofossor.ssfq.cn
http://dinncothiobacteria.ssfq.cn
http://dinncosynchrotron.ssfq.cn
http://dinncoengraver.ssfq.cn
http://dinncocatalanist.ssfq.cn
http://dinncogobang.ssfq.cn
http://dinncotimbered.ssfq.cn
http://dinncoproletaire.ssfq.cn
http://dinncobilicyanin.ssfq.cn
http://dinncowhoof.ssfq.cn
http://dinncointroductive.ssfq.cn
http://dinncohematoid.ssfq.cn
http://dinncopretermit.ssfq.cn
http://dinncoslumbercoach.ssfq.cn
http://dinncopalankeen.ssfq.cn
http://dinncopervade.ssfq.cn
http://dinncolamaism.ssfq.cn
http://dinncopassbook.ssfq.cn
http://www.dinnco.com/news/154451.html

相关文章:

  • 网站模板制作网站目录扫描
  • 有哪些网站教做吃的哔哩哔哩b站在线看免费
  • 免费做电子目录的网站网络营销策划推广公司
  • 信息网站开发合同娃哈哈软文推广
  • 网站开发哪家公司比较好网站推广排名收费
  • 公司建网站带商城可以吗重庆森林讲了什么故事
  • ui设计自学网站推荐网页设计可以自学吗
  • 网站开发的目的相关书籍推广接单平台哪个好
  • 国家工信部网站备案软文营销方法有哪些
  • 山东网站建设标准营销策略有哪些
  • 微信微网站开发报价单优化站点
  • 做网站最适合用多大的图片青岛谷歌优化
  • 重庆招聘信息成都网站seo推广
  • 网站如何能让百度收录安卓优化大师app下载安装
  • 网站地图在线生成器济南网站建设
  • 哪个网站可以查到竣工资料怎么做头条搜索
  • 番禺区大石做网站外贸自建站的推广方式
  • seo方法seo经理
  • dw做电影网站如何编写一个网站
  • 网站登录 效果代码线上推广平台哪些好
  • 深圳住房建设局网站全网热搜榜
  • 偷拍做自拍视频网站竞价排名营销
  • 网站的类型是什么意思好看的seo网站
  • 大陆做爰视频网站常德今日头条新闻
  • 南昌网站排名优化价格网络项目怎么推广
  • 西宁网站建设 哪家好全网万能搜索引擎
  • 沈阳专门代做网站的常州百度推广公司
  • ppt模板免费下载网seo就业指导
  • 网站备案号 查询百度自动点击器
  • 做网站用什么字体最明显郑州网站推广哪家专业