为已定义变量创建别名

定义变量时,在变量前加上‘&’符号,就表示定义一个引用。首先声明,引用只是为引用对象创建了别名,而并未开辟新的内存。
先看下面一段代码:

1
2
3
4
5
6
7
double i = 42.0;
double &r = i;
std::cout << "the value of i is " << i << std::endl;
std::cout << "the value of r is " << r << std::endl;
std::cout << "the address of i is "<< &i << std::endl;
std::cout << "the address of r is " << &r << std::endl;

阅读全文 »