温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++缺省参数的概念是什么

发布时间:2022-01-10 14:25:53 来源:亿速云 阅读:141 作者:iii 栏目:开发技术

这篇文章主要介绍了C++缺省参数的概念是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C++缺省参数的概念是什么文章都会有所收获,下面我们一起来看看吧。

    一、缺省参数概念

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

    #include<iostream>
    using namespace std;
    void TestFunc(int a = 0)//参数缺省值
    {
    	cout << a << endl;
    }
    int main()
    {
    	TestFunc();//没有指定实参,使用缺省值
    	TestFunc(10);//指定实参,使用实参
    	return 0;
    }

    有什么用 

    比如在 C 语言中有个很苦恼的问题是写栈时,不知道要开多大的空间,之前我们是如果栈为空就先开 4 块空间,之后再以 2 倍走,如果我们明确知道要很大的空间,那么这样就只能一点一点的接近这块空间,就太 low 了。但如果我们使用缺省,明确知道不需要太大时就使用默认的空间大小,明确知道要很大时再传参

    #include<iostream>
    using namespace std;
    namespace WD
    {
    	struct Stack
    	{
    		int* a;
    		int size;
    		int capacity;	
    	};
    }
    using namespace WD;
    void StackInit(struct Stack* ps)
    {
    	ps->a = NULL; 
    	ps->capacity = 0;
    	ps->size = 0;
    }
    void StackPush(struct Stack* ps, int x)
    {
    	if(ps->size == ps->capacity)
    	{
    		//ps->capacity *= 2;//err
    		ps->capacity == 0 ? 4 : ps->capacity * 2;//这里就必须写一个三目
    	}
    }
    void StackInitCpp1(struct Stack* ps, int defaultCP)
    {
    	ps->a = (int*)malloc(sizeof(int) * defaultCP);
    	ps->capacity = 0;
    	ps->size = defaultCP;
    }
    void StackInitCpp2(struct Stack* ps, int defaultCP = 4)//ok
    {
    	ps->a = (int*)malloc(sizeof(int) * defaultCP);
    	ps->capacity = 0;
    	ps->size = defaultCP;
    }
    int main()
    {
    	//假设明确知道这里至少需要100个数据到st1
    	struct Stack st1; 
    	StackInitCpp1(&st1, 100);
    	//假设不知道st2里需要多少个数据 ———— 希望开小点
    	struct Stack st2;  
    	StackInitCpp2(&st1);//缺省
    	return 0;
    }

    二、缺省参数分类

    全缺省参数 

    void TestFunc(int a = 10, int b = 20, int c = 30)
    {
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    	cout << "c = " << c << endl;
    	cout << endl;
    }
    int main()
    {
    	//非常灵活,
    	TestFunc();
    	TestFunc(1);
    	TestFunc(1, 2);
    	TestFunc(1, 2, 3);	
    	//TestFunc(1, , 3);//err,注意它没办法实现b不传,只传a和b,也就是说编译器只能按照顺序传
    	return 0;
    }

    注意:

    全缺省参数只支持顺序传参

    半缺省参数 

    //void TestFunc(int a, int b = 10, /*int f, - err*/ int c = 20);//err,void TestFunc(int a, int b = 10, /*int f, int x = y, -> err*/ int c = 20){cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl;cout << endl;}int main(){//TestFunc();//err,至少得传一个,这是根据形参有几个非半缺省参数确定的TestFunc(1);TestFunc(1, 2);TestFunc(1, 2, 3);return 0;}//void TestFunc(int a, int b = 10, /*int f, - err*/ int c = 20);//err,
    void TestFunc(int a, int b = 10, /*int f, int x = y, -> err*/ int c = 20)
    {
    	cout << "a = " << a << endl;
    	cout << "b = " << b << endl;
    	cout << "c = " << c << endl;
    	cout << endl;
    }
    int main()
    {
    	//TestFunc();//err,至少得传一个,这是根据形参有几个非半缺省参数确定的
    	TestFunc(1);
    	TestFunc(1, 2);
    	TestFunc(1, 2, 3);	
    	return 0;
    }
    //a.hvoid TestFunc(int a = 10);//a.cppvoid TestFunc(int a = 20){}

    注意:

    • 半缺省参数必须从右往左依次来给出,且不能间隔着给

    • 缺省参数不能在函数声明和定义中同时出现

    • 缺省值必须是常量或者全局变量

    • C 语言不支持缺省

    关于“C++缺省参数的概念是什么”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“C++缺省参数的概念是什么”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    c++
    AI