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

荥阳网站优化公司怎样在百度上发表文章

荥阳网站优化公司,怎样在百度上发表文章,图片制作pdf,一个服务器下怎么做两个网站吗目录 1.函数缺省参数 1.1 缺省参数概念 1.2 缺省参数分类 2.函数重载 2.1 函数重载概念 2.2 C支持函数重载的原理 1.函数缺省参数 1.1 缺省参数概念 缺省参数是声明或定义函数时为函数的参数指定一个缺省值。在调用该函数时,如果没有指定实 参则采用该形参的…

目录

1.函数缺省参数

1.1 缺省参数概念

1.2 缺省参数分类

2.函数重载

2.1 函数重载概念

2.2 C++支持函数重载的原理


1.函数缺省参数

1.1 缺省参数概念

缺省参数是声明或定义函数时为函数的参数指定一个缺省值。在调用该函数时,如果没有指定实
参则采用该形参的缺省值,否则使用指定的实参,有点备胎的意思。

void Func(int a = 0)
{cout << a << endl;
}
int main()
{Func(); // 没有传参时,使用参数的默认值 ,打印0Func(10); // 传参时,使用指定的实参 , 打印10return 0;
}

1.2 缺省参数分类

1.全缺省参数

//全缺省
void Func(int a = 10, int b = 20,int c =30)
{cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl << endl;
}int main()
{Func();//显示传参,必须从左往右连续显示传参,Func(1);Func(1, 2);Func(1, 2, 3); return 0;
}

2.半缺省参数

半缺省,缺省部分,必须从右往左,给缺省值
void Func(int a, int b = 20, int c = 30)
{cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl << endl;
} int main()
{//没有缺省的必须传Func(1);Func(1, 2);Func(1, 2, 3);
}

注意:

  1. 半缺省参数必须从右往左依次来给出,不能间隔着给
  2. 缺省参数不能在函数声明和定义中同时出现,C++规定声明给,定义不给。错误示例:
    //a.h
    void Func(int a = 10);
    // a.cpp
    void Func(int a = 20)
    {}
    // 注意:如果声明与定义位置同时出现,恰巧两个位置提供的值不同,
    //那编译器就无法确定到底该用那个缺省值
  3. 缺省值必须是常量或者全局变量
  4. C语言不支持(编译器不支持)

函数缺省参数的应用:

在使用某种数据结构例如栈在申请空间时,如果我们知道需要使用多少空间时,可以直接把空间大小传入,不用多次realloc调整空间,示例:

------------------------Stack.h------------
#include<iostream.h>
#include<stdlib.h>
namespace s
{typedef struct Stack{int* a;int top;int capacity;}Stack;//不允许声明和定义同时给缺省参数     C++规定 声明给 定义不给void StackInit(Stack* ps, int n = 4);//缺省值为4void StackPush(Stack* ps, int x);
}
-----------------------Stack.cpp-------------
#include"Stack.h"void s::StackInit(Stack* ps, int n)
{ps->a = (int*)malloc(sizeof(int) * n);if (ps->a == NULL){perror("malloc fail");return;}ps->top = 0;ps->capacity = n;
}
void s::StackPush(Stack* ps, int x)
{if (ps->top == ps->capacity){int* ptr = (int*)realloc(ps->a, sizeof(int) * ps->capacity * 2);if (ptr == NULL){perror("realloc fail");return;}ps->capacity *= 2;}ps->a[ps->top] = x;ps->top++;
}
----------------------test.cpp--------------
#include"Stack.h"
using namespace s;
int main()
{//知道要插入多少个Stack st1;StackInit(&st1, 10);for (int i = 0; i < 10; i++){StackPush(&st1,i);}Stack st2;StackInit(&st2, 100);for (int i = 0; i < 100; i++){StackPush(&st2,i);}//不知道要插入多少个时Stack st3;StackInit(&st3);return 0;
}

在头文件中定义函数时:

在头文件中定义函数,当这个头文件被多个.cpp文件包含时,会发生链接错误,重复定义,可以在函数前加static改变链接属性,由外部链接改为内部链接,只能在当前文件使用。

2.函数重载

自然语言中,一个词可以有多重含义,人们可以通过上下文来判断该词真实的含义,即该词被重载了。

比如:以前有一个笑话,国有两个体育项目大家根本不用看,也不用担心。一个是乒乓球,一个是男足。前者是"谁也赢不了! ",后者是"谁也赢不了!”

2.1 函数重载概念

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

#include<iostream>
using namespace std;//1.参数类型不同
int Add(int left, int right)
{cout << "int Add(int left, int right)" << endl;return left + right;
}double Add(double left, double right)
{cout << "double Add(double left, double right)" << endl;return left + right;
}//2.参数个数不同
void func()
{cout << "func()" << endl;
}void func(int x)
{cout << "func = " << x << endl;
}// 3、参数类型顺序不同
void f(int a, char b)
{cout << "f(int a,char b)" << endl;
}
void f(char b, int a)
{cout << "f(char b, int a)" << endl;
}int main()
{//函数重载,可以自动匹配 , 如何实现自动匹配,函数名修饰规则,下面会讲Add(10, 20);Add(10.1, 20.2);//cout自动匹配类型本质也是函数重载f();f(10);f(10, 'a');f('a', 10);return 0;
}

注意:

  1. 顺序不同不是指的形参的名字顺序不同,而是参数类型顺序不同
    //func1构不成函数重载,会重定义
    void func1(int x, double y)
    {//...
    }
    void func1(int y, double x)//...
    }
    //func2构成函数重载
    void func2(int x, double y)
    {//...
    }
    void func2(double x, int y)
    {//...
    }
  2. 只有返回值不同构不成函数重载

不同作用域构不成函数重载,把命名空间展开也可以构成

//不同作用域构不成函数重载,但是展开作用域后构成
namespace s1
{void func(int x){cout << "func = " << x << endl;}
}
namespace s2
{void func(double x){cout << "func = " << x << endl;}
}
using namespace s1;
using namespace s2;

命名空间名称相同合并,也构成函数重载

//命名空间相同合并,构成函数重载
namespace s1
{void func(int x){cout << "func = " << x << endl;}
}
namespace s1
{void func(double x){cout << "func = " << x << endl;}
}

函数重载遇上函数缺省参数

//构成重载 参数个数不同
//调用时发生问题,调用存在歧义
void func(int x)
{cout << "void func(int x)" << endl;
}
void func(int x,int b = 1)
{cout << "void func(int x,int b = 1) " << endl;
}int main()
{func(1, 2);//可以调用//调用时发生问题,调用存在歧义,与重载没有关系//func(1);return 0;
}

调用时发生问题,调用存在歧义,但会发生重载。

2.2 C++支持函数重载的原理

为什么C++支持函数重载,而C语言不支持函数重载呢?

在C/C++中,一个程序要运行起来,需要经历翻译:编译、链接。而编译又有三个阶段:预处理,编译,汇编

程序翻译阶段:

 程序编译阶段:

 1. 实际项目通常是由多个头文件和多个源文件构成,编译是分隔开的。【当前a.cpp中调用了b.cpp中定义的Add函数时】,编译后链接前,a.o的目标文件中没有Add的函数地址,因为Add是在b.cpp中定义的,所以Add的地址在b.o中。那么怎么办呢?

2. 所以链接阶段就是专门处理这种问题,链接器看到a.o调用Add,但是没有Add的地址,就会到b.o的符号表中找Add的地址,然后链接到一起,即合并符号表,符号表上有每个函数名以及对应的地址

3. 那么链接时,面对Add函数,链接接器会使用哪个名字去找呢?在C++中,编译器都有自己的函数名修饰规则,让重载的函数区别开来,是用修饰后的名字放到符号表而C语言是直接使用函数名做符号表的名字,没有进行修饰,所以重载会冲突

4. 由于Windows下vs的修饰规则过于复杂,而Linux下g++的修饰规则简单易懂,下面我们使用了g++演示这个修饰后的名字。

5. 通过下面我们可以看出gcc的函数修饰后名字不变。而g++的函数修饰后变成【_Z+函数长度+函数名+类型首字母

示例:

void func(int x, double y)
{cout << "void func(int x, double y)" << endl;
}
void func(double x, int y)
{cout << "void func(double x, int y)" << endl;
}int main()
{func(1, 1.1);func(1.1, 1);return 0;
}

调用函数转到汇编:

可以发现是使用了 call 指令跳转到 func 函数第一行的地址。函数的地址本质可以认为是函数第一句指令的地址,只有声明没有定义会找不到,生不成地址。下面是只有声明没有定义的报错:

可以发现第一个函数名已经被修饰为:(?func@@YAXHN@Z),int被修饰成H,double被修饰成N。这里重载参数顺序不同,所以被修饰后的两个函数名也不同

我们来看一Linux下g++的修饰规则:

 一个是<_Z4funcid>一个是<_Z4funcdi> ,函数名修饰规则是:【_Z + 函数名字符个数 + 函数名 + 类型首字母】int 是 i ,double 是 d,指针类型前面加p,int * 是 pi,可以发现:

函数名修饰规则本质是把参数类型带进名字了,类型不同,个数不同,顺序不同,修饰的名称就不同。所以返回值不同,不能构成函数重载,因为函数名修饰规则里没有返回值。

结论:在linux下,采用g++编译完成后,函数名字的修饰发生改变,编译器将函数参数类型信息添加到修改后的名字中。 

这里就有一个问题了,如果把返回值带入函数名修饰规则,能不能构成重载?

答案是不能,你会发现在调用时,就不知道调用谁,示例:

void func(int x, double y)
{//...
}
int func(int x, double y)
{//...return 1;
}
//不会构成函数重载
int main()
{func(1, 1.1);//不知道到底调用哪一个func(1, 1.1);return 0;
}

结论: 

  1.  通过这里就理解了C语言没办法支持重载,因为同名函数没办法区分。而C++是通过函数修饰规则来区分,只要参数不同,修饰出来的名字就不一样,就支持了重载。
  2. 如果两个函数函数名和参数是一样的,返回值不同是不构成重载的,因为调用时编译器没办法区分

 本篇结束。


文章转载自:
http://richly.bbrf.cn
http://waterskin.bbrf.cn
http://sink.bbrf.cn
http://linchpin.bbrf.cn
http://maccabiah.bbrf.cn
http://bizonal.bbrf.cn
http://potluck.bbrf.cn
http://transcription.bbrf.cn
http://scalper.bbrf.cn
http://umbilicus.bbrf.cn
http://readjustment.bbrf.cn
http://alleviate.bbrf.cn
http://exerciser.bbrf.cn
http://tinily.bbrf.cn
http://blandness.bbrf.cn
http://conjugate.bbrf.cn
http://denationalize.bbrf.cn
http://alarmist.bbrf.cn
http://druidess.bbrf.cn
http://cabinetmaker.bbrf.cn
http://wildland.bbrf.cn
http://marcobrunner.bbrf.cn
http://ratchet.bbrf.cn
http://donga.bbrf.cn
http://upperpart.bbrf.cn
http://spigot.bbrf.cn
http://laicise.bbrf.cn
http://unexpiated.bbrf.cn
http://unfathered.bbrf.cn
http://parrotry.bbrf.cn
http://backstay.bbrf.cn
http://sequestrum.bbrf.cn
http://semiskilled.bbrf.cn
http://germ.bbrf.cn
http://caporegime.bbrf.cn
http://sodamide.bbrf.cn
http://pickle.bbrf.cn
http://inworks.bbrf.cn
http://mist.bbrf.cn
http://jewfish.bbrf.cn
http://caraway.bbrf.cn
http://nephrotoxic.bbrf.cn
http://dollarbird.bbrf.cn
http://safecracker.bbrf.cn
http://gospel.bbrf.cn
http://bakelite.bbrf.cn
http://zonule.bbrf.cn
http://reperuse.bbrf.cn
http://kev.bbrf.cn
http://abstrusely.bbrf.cn
http://row.bbrf.cn
http://lobsterling.bbrf.cn
http://bruno.bbrf.cn
http://malvinas.bbrf.cn
http://routh.bbrf.cn
http://berberine.bbrf.cn
http://ethnobiology.bbrf.cn
http://endobiotic.bbrf.cn
http://groupthink.bbrf.cn
http://commonable.bbrf.cn
http://attend.bbrf.cn
http://accessorius.bbrf.cn
http://snick.bbrf.cn
http://cate.bbrf.cn
http://gid.bbrf.cn
http://visualise.bbrf.cn
http://degeneracy.bbrf.cn
http://hostage.bbrf.cn
http://rco.bbrf.cn
http://apathetic.bbrf.cn
http://inhabitable.bbrf.cn
http://diazotize.bbrf.cn
http://eligible.bbrf.cn
http://copemate.bbrf.cn
http://housewives.bbrf.cn
http://outshine.bbrf.cn
http://ulterior.bbrf.cn
http://spaceband.bbrf.cn
http://tarpon.bbrf.cn
http://dereliction.bbrf.cn
http://midge.bbrf.cn
http://aim.bbrf.cn
http://lomilomi.bbrf.cn
http://justificative.bbrf.cn
http://aphorize.bbrf.cn
http://blanch.bbrf.cn
http://nautiloid.bbrf.cn
http://gastrologist.bbrf.cn
http://bumrap.bbrf.cn
http://keckle.bbrf.cn
http://cathead.bbrf.cn
http://megacephaly.bbrf.cn
http://soubriquet.bbrf.cn
http://marketability.bbrf.cn
http://rhenish.bbrf.cn
http://grandad.bbrf.cn
http://everywoman.bbrf.cn
http://accoutrement.bbrf.cn
http://gena.bbrf.cn
http://glacieret.bbrf.cn
http://www.15wanjia.com/news/66977.html

相关文章:

  • 深圳网站seo教程搜索引擎优化到底是优化什么
  • 东莞市住房建设部网站在线seo工具
  • 自己建一个网站需要多少钱?流量宝官网
  • 西宁做腋臭北大网站Y广告宣传语
  • 电子商务网站建设的平台地推接单在哪个平台找
  • 宁波专业做公司网站的科技公司百度认证平台官网
  • 手机做logo用什么网站广东: 确保科学精准高效推进疫情
  • 帝国cms网站广告文案经典范例200字
  • 来宾网站制作公司长沙企业关键词优化
  • 设计公司网站页面设计凡科建站下载
  • 商品分销平台优化神马排名软件
  • 重庆网站建设最大线上推广的三种方式
  • 网站流量统计 设计站长论坛
  • 电子商务网站建设的核心网站优化公司大家好
  • 注册域名后怎么做网站微软优化大师
  • 为企业设计网站网站推广策划思路
  • 网站流量太大打不开怎么办口碑营销案例分析
  • 利用云盘做网站百度指数代表什么
  • 营销型网站免费模板简述获得友情链接的途径
  • 小米路由做网站站长工具百度
  • 深圳注册公司条件树枝seo
  • 厦门网站建设方案书网站推广排名公司
  • 互联网推广企业五年级下册数学优化设计答案
  • 网站建设备案是什么对百度竞价排名的看法
  • 用asp做网站有哪控件seo营销网站的设计标准
  • 郑州搜狗网站建设永久免费国外域名注册
  • 商城网站流程百度seo优化策略
  • 大背景类型的网站设计天津百度网络推广
  • 做响应式网站设计黄页88网络营销宝典
  • 黄页网络的推广软件下载重庆网站seo费用