一:
- C++函数的形式参数被定义为引用类型,则称为引用参数。
- 变量是可以保存数据的内存位置的名称。当使用变量时,实际上就是访问存储在分配给它的内存位置的数据。
- 引用变量是变量的另一个别名,它没有自己的存储数据的内存位置,它访问的是另一个变量的内存位置。对引用变量作出的更改,实际上都是对它所引用的变量内存位置中存储数据的更改。
- 当使用引用变量作为形参时,它将变为实参列表中相应变量的别名,对形参进行的任何更改都将真正更改正在调用它的函数中的变量。当以这种方式将数据传递给形参时,该实参被称为按引用传递
- 对形参值进行的任何更改都不会影响原始实参的值。
注意:1.引用要在定义时初始化且不可重新赋值
2.可以有空指针,但不可以有空引用
二:
1、编写一个函数,用于两个整数的交换,并在主程序中测试之。
#include<iostream>
using namespace std;
void exchange(int x,int y)
{
int t;t=x;x=y;y=t;
}
void main(){
int x,y;
cout<<"交换前:x=";
cin>>x;
cout<<"y=";
cin>>y;
exchange(x,y);
cout<<"交换后:"<<"x="<<x<<" y="<<y<<endl;
}
交换前:x=3 y=5
交换后:x=3 y=5
修改以上程序,使用指针做参数,使其真正实现两个数的交换
#include<iostream>
using namespace std;
void exchange(int *pa, int *pb){
int t;
t=*pa;
*pa=*pb;
*pb=t;
}
void main(){
int x,y;
cout<<"交换前:x=";
cin>>x; cout<<"y=";
cin>>y;
exchange(&x,&y);
cout<<"交换后:"<<"x="<<x<<" y="<<y<<endl;
}
交换前:x=3 y=6
交换后:x=6 y=3
修改以上程序,使用引用做参数,使其真正实现两个数的交换
#include<iostream>
using namespace std;
void exchange(int &a, int &b){
int t;
t=a;
a=b;
b=t;
void main(){
int x,y;
cout<<"交换前:x=";
cin>>x;
cout<<"y=";
cin>>y;
exchange(x,y);
cout<<"交换后:"<<"x="<<x<<" y="<<y<<endl;
}
交换前:x=5 y=7
交换后:x=7 y=5
2、不使用函数,在主程序中实现动态数组分配和输出功能
#include<iostream>
using namespace std;
void main(){
int n;
int *p=0;
cout<<"please input the length of the array:";
cin>>n;
p=new int[n];
if(p==0){
cout<<"allocate failure"<<endl;
exit(1);
}
for(int i=0;i<n;i++)
p[i]= 0;
cout<<"output the array : "<<endl;
for( i=0;i<n;i++)
cout<<p[i]<< " ";
cout<<endl;
delete []p;
}
输出:
please input the length of the array:4
output the array :
0 0 0 0
3、改进该程序,将动态分配数组及其初始化的操作封装成函数。思考以下程序为什么给p动态数组分配内存空间失败?
#include <iostream>
using namespace std;
void allocate(int *pt,int len);
void main(){
int n; // 定义数组元素的个数
int i;
int *p=0;
cout<< "please input the length of the array : ";
cin>>n;
allocate(p,n);
cout<<"output the array : "<<endl;
for( i=0;i<n;i++)
cout<<p[i]<< " ";
cout<<endl;
delete []p; // 释放内存空间
}
void allocate(int *pt,int len){
pt=new int[len];
if (pt==0)
{
cout<< " allocate failure"<<endl;
exit(1);
} // 分配内存空间
for( int i=0;i<len;i++)
pt[i]= 0;
}
输出:
please input the length of the array : 3
output the array :
4、修改以上程序,使用指针做函数返回值,将被调函数执行时动态开辟的内存块首地址返回给主程序
#include <iostream>
using namespace std;
int * allocate(int len);
void main(){
int n; // 定义数组元素的个数
int i;
int *p=0;
cout<< "please input the length of the array : ";
cin>>n;
p=allocate(n);
cout<<"output the array : "<<endl;
for( i=0;i<n;i++)
cout<<p[i]<< " ";
cout<<endl;
delete []p; // 释放内存空间
}
int * allocate(int len){
int *pt=0;
pt=new int[len];
if (pt==0)
{
cout<< " allocate failure"<<endl;
exit(1);
} // 分配内存空间
for( int i=0;i<len;i++)
pt[i]= 0;
return pt;
}
输出:
please input the length of the array : 3
output the array :
0 0 0
5、修改以上程序,使用二级指针做函数形参,使其指向实参指针,通过形参指针间接的操作实参指针,使实参指针指向被调函数执行时动态开辟的内存块
#include <iostream>
using namespace std;
void allocate(int **pt,int len);
void main(){
int n; // 定义数组元素的个数
int i;
int *p=0;
cout<< "please input the length of the array : ";
cin>>n;
allocate(&p,n);
cout<<"output the array : "<<endl;
for( i=0;i<n;i++)
cout<<p[i]<< " ";
cout<<endl;
delete []p; // 释放内存空间
}
void allocate(int **pt,int len){
*pt=new int[len];
if (*pt==0)
{
cout<< " allocate failure"<<endl;
exit(1); } // 分配内存空间
for( int i=0;i<len;i++)
(*pt)[i]= 0;
}
输出:
please input the length of the array : 4
output the array :
0 0 0 0
6、以上方法的缺点是程序显得不自然,可读性不好,修改以上程序,使用引用做形参,使形参是实参指针的别名,通过形参操作实参指针,程序更自然,可读性更好
#include <iostream>
using namespace std;
void allocate(int * &pt,int len);
void main(){
int n; // 定义数组元素的个数
int i;
int *p=0;
cout<< "please input the length of the array : ";
cin>>n;
allocate(p,n);
cout<<"output the array : "<<endl;
for( i=0;i<n;i++)
cout<<p[i]<< " ";
cout<<endl;
delete []p; // 释放内存空间
}
void allocate(int * &pt,int len){
pt=new int[len];
if (pt==0)
{
cout<< " allocate failure"<<endl;
exit(1); } // 分配内存空间
for( int i=0;i<len;i++)
pt[i]= 0;
}
输出:please input the length of the array : 5
output the array :
0 0 0 0 0
7、采用模块化思想,将以上程序的其他功能也封装成函数,注意函数定义和函数调用时参数的设置
#include <iostream>
using namespace std;
void allocate(int * &pt,int len);
void output(int * &pt,int len);
void main(){
int n; // 定义数组元素的个数
int *p=0;
cout<< "please input the length of the array : ";
cin>>n;
allocate(p,n);
output(p,n);
delete []p; // 释放内存空间
}
void allocate(int * &pt,int len) {
pt=new int[len];
if (pt==0)
{
cout<< " allocate failure"<<endl;
exit(1); } // 分配内存空间
for( int i=0;i<len;i++)
pt[i]= 0;} void output(int * &pt,int len) //也可以写成void output(int * pt,int len),思考为什么。
{
int i;
cout<<"output the array : "<<endl;
for( i=0;i<len;i++)
cout<<pt[i]<< " ";
cout<<endl;
}
输出:please input the length of the array : 6
output the array :
0 0 0 0 0 0
8、给以上程序完善得实用些,增加输入功能
#include <iostream>
using namespace std;
void allocate(int * &pt,int len);
void output(int * &pt,int len);
void input(int * &pt,int len);
void main(){
int n; // 定义数组元素的个数
int *p=0;
cout<< "please input the length of the array : ";
cin>>n;
allocate(p,n);
output(p,n);
input(p,n);
output(p,n);
delete []p; // 释放内存空间
}
void allocate(int * &pt,int len){
pt=new int[len];
if (pt==0)
{
cout<< " allocate failure"<<endl;
exit(1); } // 分配内存空间
for( int i=0;i<len;i++)
pt[i]= 0;
}
void output(int * &pt,int len) //也可以写成void output(int * pt,int len)
{
int i;
cout<<"output the array : "<<endl;
for( i=0;i<len;i++)
cout<<pt[i]<< " ";
cout<<endl;
}
void input(int * &pt,int len) //也可以写成void input(int * &pt,int len)
{
int i;
cout<<"intput the array : "<<endl;
for( i=0;i<len;i++) {
cout<<"pt["<<i<<"]:";
cin>>pt[i];
}
cout<<endl;
}
输出:please input the length of the array : 5
output the array :0 0 0 0 0
intput the array :
pt[0]:4
pt[1]:3
pt[2]:2
pt[3]:5
pt[4]:7
output the array :
4 3 2 5 7
心得体会:
1、声明一个引用,不是新定义一个变量,它只是表示该引用名是目标变量名的一个别名,本身不是一种数据类型,而且引用不占存储单元。
2、用引用传递函数的参数,能保证参数传递中不产生副本,提高传递的效率,且通过const的使用,保证了引用传递的安全性。
3、被函数返回的引用只是作为一 个临时变量出现,而没有被赋予一个实际的变量,那么这个引用所指向的空间(由new分配)就无法释放。