您当前的位置: 首页 >  c++

txwtech

暂无认证

  • 2浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ca55a_c++_函数参数传递_非引用形参

txwtech 发布时间:2020-02-06 15:15:32 ,浏览量:2

/*ca55a_c++_函数参数传递_非引用形参 txwtech 非引用形参,传参数,就是copy 1.普通形参 * 非const形参 * const形参,不能修改 2.指针形参 * 非const指针形参 * const指针形参

//AddOne2(a2);//error C2664: “void AddOne2(int *)”: 无法将参数 1 从“const int”转换为“int *” 不能把const传给非const 非const实参可以传给const形参 const可以传给const

3.复制实参的局限性 非引用形参,传参数,就是copy,如果一个数组比较大,比如int a[1000000]; 有些情况下,类里面不能复制实参 class Dog { int a[100000]; } void doA(Dog dog) { //传递数组a时,执行拷贝,就会开销很大,花费时间长。 }

引用形参。。。

*/

/*ca55a_c++_函数参数传递_非引用形参
txwtech
非引用形参,传参数,就是copy
1.普通形参
* 非const形参
* const形参,不能修改
2.指针形参
* 非const指针形参
* const指针形参

//AddOne2(a2);//error C2664: “void AddOne2(int *)”: 无法将参数 1 从“const int”转换为“int *”
不能把const传给非const
非const实参可以传给const形参
const可以传给const

3.复制实参的局限性
非引用形参,传参数,就是copy,如果一个数组比较大,比如int a[1000000];
有些情况下,类里面不能复制实参
class Dog
{
int a[100000];
}
void doA(Dog dog)
{
//传递数组a时,执行拷贝,就会开销很大,花费时间长。
}

引用形参。。。

*/

#include 

using namespace std;

void AddOne(int x)//非引用形参,传参数,就是copy
{
	x = x + 1;
}
//传指针的copy,地址的拷贝
void AddTwo(int *px)//指针形参,(指针是非引用)
{
	*px = *px + 2;
}
//引用形参,传递c本身,
void AddThree(int& x)//引用形参,传递c本身,
{
	x = x + 3;
}
int add(int x, int y)
{
	return x + y;
}
int add2(const int x, const int y)//const形参
{
	return x + y;
}
//非引用const 形参,传参数,就是copy
void AddOne1(const int x)//非引用const 形参,传参数,就是copy
{
	//x = x + 1; //const值不能修改
}
//demo3
void AddOne2( int *ip)//
{
	*ip = *ip + 1;
}
//const指针,值不能修改
void AddTwo2(const int *px)//const指针,值不能修改
{
	//*px = *px + 2;//const指针,值不能修改
}
int add3(const int *px, const int *py)
{
	return *px + *py;
}
//函数重载,函数名一样,参数不一样
void fcn(int i)//函数重载,函数名一样,参数不一样
{
	cout             
关注
打赏
1665060526
查看更多评论
0.0501s