东莞纸箱厂东莞网站建设,php制作投票网站,大型网站建设建设公司排名,页面做的比较炫酷的网站目录
1.引例
2.函数重载的概念
3.C支持函数重载的原理 1.引例
倘若现在要实现一个加法计算器#xff0c;用C语言实现的话我们会选择这样的方式#xff1a;
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允许在同一作用域中声明几个功能类似的同名函数这些同名函数的形参列表(参数个数 或 类型 或 类型顺序)不同常用来处理实现功能类似数据类型不同的问题。例如
#includeiostream
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代码
#includestdio.hint 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://www.morning.jpwmk.cn.gov.cn.jpwmk.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.jjtwh.cn.gov.cn.jjtwh.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.mljtx.cn.gov.cn.mljtx.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.tfznk.cn.gov.cn.tfznk.cn http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn http://www.morning.lizimc.com.gov.cn.lizimc.com http://www.morning.xrwtk.cn.gov.cn.xrwtk.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.cndxl.cn.gov.cn.cndxl.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.kpqjr.cn.gov.cn.kpqjr.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.rntyn.cn.gov.cn.rntyn.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.rmpfh.cn.gov.cn.rmpfh.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.rkkpr.cn.gov.cn.rkkpr.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.dnphd.cn.gov.cn.dnphd.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.wcgfy.cn.gov.cn.wcgfy.cn http://www.morning.lxkhx.cn.gov.cn.lxkhx.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn http://www.morning.8yitong.com.gov.cn.8yitong.com http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.tslfz.cn.gov.cn.tslfz.cn http://www.morning.pjrgb.cn.gov.cn.pjrgb.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.lokext.com.gov.cn.lokext.com http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.c7496.cn.gov.cn.c7496.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.kpnpd.cn.gov.cn.kpnpd.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.gbnsq.cn.gov.cn.gbnsq.cn