C++基础知识(引用)

引用

1、引用的基本语法

     引用是C++对C的一种重要扩充。C++增添了一种给函数传递地址的途径,即按引用传递(pass-by-reference).

Note:(1)变量名实际上就是一段连续内存空间的别名,是一个标号(门牌号);

           (2)程序中通过变量来申请并命名内存空间;

           (3)通过变量的名字可以使用存储空间。

基本语法: Type &ref = val;

注意事项:&在此不是求地址运算,而是起标识作用;

                  类型标识符是指目标变量的类型;

                  必须在声明引用变量时进行初始化;

                  引用初始化之后不能改变;

                  不能有NULL引用,必须确保引用是和一个合法的存储单元关联;

                  可以建立对数组的引用。

#include<iostream>
using namespace std;

int main()
{
	//引用就是给变量起别名
	int a = 12;
	int &c = a;//为变量a起别名s【声明变量a的一个引用】
	int &d = a;

	cout << &a << ' ' << &c << ' ' << &d << endl;

	int& e = c;
	int f = 14;
	c = f;  //f的值赋给c
	cout << a << endl;
	c = 13;
	cout << a << endl;

	system("pause");
	return 0;
}

引用做参数

#include<iostream>
using namespace std;

void fun(int& a) //引用是一种直接的操作
{
	a = 13;
	//cout << a << endl;
}
void fun1(int a)      
{
	a = 14;
	cout << "fun1:" << a << endl;
}
void fun2(int *a) //指针是一种间接的操作
{
	*a = 15;
}
int main()
{
	int b = 12;
	//fun(b);
	fun2(&b);
	cout << b << endl;
	system("pause");
	return 0;
}

引用的基本语法

#include<iostream>
using namespace std;

int main()
{
	//常量的引用
	const int& a = 12;  //常量不能进行修改(写操作)
	cout << a << endl;

	//数组的引用
	int arr[12];
	int(&p)[12] = arr; //引用 类型
	p[2] = 12;
	cout << arr[2] << endl;

	int arr2[2][3];//类型一定匹配
	int(&p2)[2][3] = arr2;
	p2[1][2] = 123;
	cout << arr2[1][2] << endl;

	//指针的引用
	int b = 12;
	int *point = &b;
	int* (&p3) = point;
	*p3 = 34;
	cout << *point << ' ' << b << endl;
	system("pause");
	return 0;
}

数组的两种引用对比

//对数组建立引用
void test03()
{
	int arr[10];
	for (int i = 0; i < 10; i++)
	{
		arr[i] = i;
	}
	//给数组起别名
	int(&pArr)[10] = arr;  //声明一个指向大小为10的整型数组的变量的引用
	for (int i = 0; i < 10; i++)
	{
		cout << pArr[i] << " ";
	}
	cout << endl;

	//第二种方式 起别名
	typedef int(ARRAYEF)[10]; //一个具有10个元素的int类型数组
	ARRAYEF & pArr2 = arr;
	for (int i = 0; i < 10; i++)
	{
		cout << pArr2[i] << " ";
	}
	cout << endl;

}

指针的引用

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

struct Person
{
	int m_Age;
};

void allocatMemory(Person **p)
{
	*p = (Person *)malloc(sizeof(Person)); //**p具体的Perosn对象  *p对象的指针 p指针的指针
	(*p)->m_Age = 100;
}


void test01()
{
	Person * p = NULL;
	allocatMemory(&p);
	cout << "p的年龄: " <<p->m_Age << endl;
}

//利用指针引用开辟空间
void allocatMemoryRef(Person* &p)
{
	p = (Person*)malloc(sizeof(Person));
	p->m_Age = 50;
}

void test02()
{
	Person *p = NULL;
	allocatMemoryRef(p);
	cout << "p的年龄: " << p->m_Age << endl;
}

int main(void)
{
	test02();

	system("pause");
	return 0;
}

常量的引用

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

void test01()
{
	//int &ref = 10;//引用了不合法的内存,不可以
	const int &ref = 10; //加入const后,编译器处理方式为:int tmp=10;const int &ref=tmp;
	//ref = 10;
	int *p = (int*)&ref;
	*p = 1000;
	cout << "ref=" << ref << endl;
}

//引用使用的场景用来修饰形参
void showValue(const int &val)
{
	//val += 1000;//如果只是想显示内容,而不修饰内容,那么就要const修饰这个形参
	cout << "val=" << val << endl;
}

void test02()
{
	int a = 10;
	showValue(a);
}
int main(void)
{
	test01();
	system("pause");
	return 0;
}

引用与指针的区别

1、引用声明就要初始化,指针不用,int *p = NULL;

2、引用初始化之后就不能引用其他空间了,指针可以指向其他空间;

3、引用不占存储空间,指针占空间;

4、引用效率更高,指针是间接操作;

5、引用更安全,指针可以偏移;

6、指针更灵活,直接操作地址,指针更通用,C语言和C++都行。

&的三种作用(运算符重载现象)

1、声明变量的时候用&,表示引用;

2、变量前边加&,表示取地址;

3、数&数,表示位与运算。

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值