C++新增的复合类型————引用。
引用变量的主要用途是用作函数的形参,通过将引用变量用作参数,函数将使用原始数据,而不是其副本,这样除了指针之外引用也将为函数处理大型结构提供了一种非常方便的途径。
一、创建引用变量
#include <iostream>
using namespace std;
int main()
{
int number1 = 50,number2 = 100,number3;
int& refer = number1;
cout << "int& refer = number1" << endl;
cout << "The value of refer :" << refer << " refer at " << &refer << endl;
cout << "The value of number1:" << number1 << " number1 at " << &number1 << endl;
cout << endl << "refer = number2" << endl;
refer = number2;
cout << "The value of refer " << refer << " refer at" << &refer << endl;
cout << "The value of number1:" << number1 << " number1 at " << &number1 << endl;
cout << "The value of number2:" << number2 << " number2 at " << &number2 << endl;
cout << endl << "number3 = refer" << endl;
number3 = refer;
cout << "The value of refer " << refer << " refer at" << &refer << endl;
cout << "The value of number1:" << number1 << " number1 at " << &number1 << endl;
cout << "The value of number3:" << number3 << " number3 at " << &number3 << endl;
return 0;
}
以下是程序的输出结果:
结合上图说一下引用变量的创建规则:
1.引用变量必须被初始化
int number1;
int& refer;
refer = numb1;
这样编译器会报错,必须:
int& refer = number1;
其中“&”是一个标示符,并不是取地址,表示这是一个引用变量,即int & 是指向int的引用。
(PS:一个变量可以被多个引用绑定,但是不建议这样做)
2.一旦引用变量被初始化,refer与number1就“绑定”了。
绑定的意思是指refer与number1的数值和地址都一样。且refer不可与其他的变量再次绑定。
但是可以写出以下语句:
refer = number2;
这里只是将number2的值赋给了number1,refer还是与number1是绑定的,由输出可以看出number2的地址与number1和refer不同。
很多情况下,为了避免这种情况的发生,可以定义const引用。
const int& refer = number1;
此时,refer = number2就会被编译器报错,但是仍然可以用number1 = number2的的方式对refer的值进行更改。
同样的,可以写出以下语句:
number3 = refer;
相当于将number1的值赋给了number3
3.修改引用及其指向的变量的任意一个,另一个也会随之改变。
二、将引用用作函数参数
通过引用变量做参数,函数使用原始数据而不是原始数据的副本,这样可以省去创建副本的时间,提高效率,尤其是当原始数据是结构体或者类这种大型结构,更加节省时间。
例如简单的函数:
void swap(int & x,int & y)
{
int temp;
temp = x;
x = y;
t = temp;
}
当调用函数swap(a,b)时,x是a的引用,y是b的引用,这样更改x和y的值即更改a和b的值。
看起来和普通函数参数没差,但是引用用作函数参数时用很多特性:
double refer_cube(double & x)
{
cout <<"The temp value is : " << x << " temp x at " << &x << endl;
return x * x * x;
}
double y = refer_cube(x+3.0);
如定义了refer_cube()函数,调用时x+3.0,这在传值函数中编译时可以通过的,但是C++不可以,这是为什么呢?
需要强调一下引用的定义规则:
引用只能指向一个变量或者对象(注意引用不可以与数组绑定,想用数组当做参数只能显式的声明或者用指针),而x+3.0是一个表达式不是一个变量,违背了规则。
但是实际上,早先的有些编译器是可以通过编译的,它是做了如下处理:
1.创建一个临时变量temp并将x+3.0赋值给temp
2.引用变量x实际上与temp绑定,在函数周期结束后temp被销毁。
看了上边的规则,感觉很像是传值调用。
由于某些历史原因,C++对规则进行了调整,如果上述函数的声明变为如下形式,则可以通过编译:
double refer_cube(const double & x)
{
cout <<"The temp value is : " << x << " temp x at " << &x << endl;
return x * x * x;
}
即在引用变量前边加入const修饰符。
给出一段代码可以更明白的理解这其中的关系:
#include <iostream>
using namespace std;
double refer_cube(const double & x)
{
cout <<"The temp value is : " << x << " temp x at" << &x << endl;
return x * x * x;
}
int main()
{
double x = 5.0;
cout <<"The value of x is : " << x << " value x at" << &x << endl;
double y = refer_cube(x+3.0);
cout <<"The value of y is : " << y << " value y at" << &y << endl;
return 0;
}
运行结果如下图所示:
可以看出临时变量地址与x不同。
并且,加入const修饰后,原数据不会被修改。
(结论是,引用作为函数的参数列表时,应当尽可能的使用const。)
既然引用是一种类型,那么必定有引用类型的函数,此种函数返回一个引用。
类似于:
string & Plus(string & source,const string & s)
{
source = source + s;
return source;
}
很容易理解,不过注意和指针一样,不可以返回一个临时变量的引用,即:
string & Plus(string & source,const string & s)
{
string temp;
temp = source + s;
return temp;
}
是不允许的,虽然会通过编译但是会waring,但是执行过程中会崩溃,因为试图引用已经释放的内存,如下语句所示。
string & result = Plus(source,"++++++")
PS:需要在此处说明的是,“++++++”虽然是一个字符串常量,但是因为它相当于一个地址(即const char*类型的指针),仍然可以被const string& 类型的引用绑定
三、引用与类
基类引用可以指向派生类的引用,这大大方便了类的设计,因此引用在类的设计方面贡献很大。
——————————————————————————————————————————————————————
总结,(在函数中)何时使用引用。
1.引用的主要作用就是修改原始数据和提高程序的运行速度,因此在数据对象较大的时使用引用。
2.设计类时使用引用,并且情况允许时尽量使用const引用。
3.如果数据对象是基本内置类型(int,char..)使用指针会比较快。
4.如果数据对象是数组,只能使用指针或者显式的数组说明。
5.