Chinaunix首页 | 论坛 | 博客
  • 博客访问: 45822
  • 博文数量: 9
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 12:19
文章分类

全部博文(9)

文章存档

2017年(4)

2016年(5)

我的朋友

分类: C/C++

2017-08-15 09:58:02

关于引用和指针的区别的文章很多很多,但是总是找不到他们的根本区别,偶然在codeproject上看到这篇文章,觉得讲的挺好的,

所以翻译了下,希望对大家有帮助。原文地址:


引言

      我选择写 C++ 中的引用是因为我感觉大多数人误解了引用。而我之所以有这个感受是因为我主持过很多 C++ 的面试,并且我很少从面试者中得到关于 C++ 引用的正确答案。

       那么 c++ 中引用到底意味这什么呢?通常一个引用让人想到是一个引用的变量的别名,而我讨厌将 c++ 中引用定义为变量的别名。这篇文章中,我将尽量解释清楚, c++ 中根本就没有什么叫做别名的东东。

 

背景

  c/c++ 中,访问一个变量只能通过两种方式被访问,传递,或者查询。这两种方式是:

    1. 通过值 访问 / 传递变量

    2. 通过地址 访问 / 传递变量 这种方法就是指针

 

       除此之外没有第三种访问和传递变量值的方法。引用变量也就是个指针变量,它也拥有内存空间。最关键的是引用是一种会被编译器自动解引用的指针。很难相信么?让我们来看看吧。。。

   下面是一段使用引用的简单 c++ 代码

点击(此处)折叠或打开

  1. #include <iostream.h>
  2. int main()
  3. {
  4.     int i = 10;  // A simple integer variable
  5.     int &j = i;  // A Reference to the variable i
  6.     j++;         // Incrementing j will increment both i and j.
  7.    
  8.     // check by printing values of i and j
  9.     cout<< i << j <<endl; // should print 11 11
  10.     // Now try to print the address of both variables i and j
  11.     cout<< &i << &j <<endl;
  12.     
  13.     // surprisingly both print the same address and make us feel that they are
  14.     // alias to the same memory location.
  15.     // In example below we will see what is the reality
  16.     return 0;
  17. }

   引用其实就是 c++ 中的常量指针。表达式   int &i = j; 将会被编译器转化成 int *const i = &j; 而引用之所以要初始化是因为 const 类型变量必须初始化,这个指针也必须有所指。下面我们再次聚焦到上面这段代码,并使用编译器的那套语法将引用替换掉。(注:编译器自动解引用


点击(此处)折叠或打开

  1. #include <iostream.h>
  2. int main()
  3. {
  4.     int i = 10;      // A simple integer variable
  5.     int *const j = &i; // A Reference to the variable i
  6.     (*j)++;          // Incrementing j. Since reference variables are
  7.    
  8.     // automatically dereferenced by compiler
  9.     // check by printing values of i and j
  10.     cout<< i << *j <<endl; // should print 11 11
  11.    
  12.     // A * is appended before j because it used to be reference variable
  13.     // and it should get automatically dereferenced.
  14.     return 0; 
  15. }
   读者一定很奇怪为什么我上面这段代码会跳过打印地址这步。这里需要一些解释。因为引用变量时会被编译器自动解引用的,那么一个诸如 cout << &j << endl; 的语句,编译器就会将其转化成语句 cout << &*j << endl;   现在 &* 会相互抵消,这句话变的毫无意义,而 cout 打印的 j 值就是 i 的地址,因为其定义语句为 int *const j = &i;

 

      所以语句 cout << &i << &j << endl; 变成了 cout << &i << &*j << endl; 这两种情况都是打印输出 i 的地址。这就是当我们打印普通变量和引用变量的时候会输出相同地址的原因。

      下面给出一段复杂一些的代码,来看看引用在级联 (cascading) 中是如何运作的。


点击(此处)折叠或打开

  1. #include <iostream.h>
  2. int main()
  3. {
  4.     int i = 10; // A Simple Integer variable
  5.     int &j = i; // A Reference to the variable
  6.     // Now we can also create a reference to reference variable.
  7.     int &k = j; // A reference to a reference variable
  8.     // Similarly we can also create another reference to the reference variable k
  9.     int &l = k; // A reference to a reference to a reference variable.
  10.    
  11.     // Now if we increment any one of them the effect will be visible on all the
  12.     // variables.
  13.     // First print original values
  14.     // The print should be 10,10,10,10
  15.     cout<< i << "," << j << "," << k << "," << l <<endl;
  16.     
  17.     // increment variable j
  18.     j++;
  19.     // The print should be 11,11,11,11
  20.     cout<< i << "," << j << "," << k << "," << l <<endl;
  21.    
  22.     // increment variable k
  23.     k++;
  24.     // The print should be 12,12,12,12
  25.     cout<< i << "," << j << "," << k << "," << l <<endl;
  26.     
  27.     // increment variable l
  28.     l++;    
  29.     // The print should be 13,13,13,13
  30.     cout<< i << "," << j << "," << k << "," << l <<endl;
  31.     return 0; 
  32. }

   下面这段代码是将上面代码中的引用替换之后代码,也就是说明我们不依赖编译器的自动替换功能,手动进行替换也能达到相同的目标。


点击(此处)折叠或打开

  1. #include <iostream.h>
  2. int main()
  3. {
  4.     int i = 10; // A Simple Integer variable
  5.     int *const j = &i; // A Reference to the variable
  6.        
  7.     // The variable j will hold the address of i
  8.     // Now we can also create a reference to reference variable.
  9.     int *const k = &*j; // A reference to a reference variable
  10.     // The variable k will also hold the address of i because j
  11.     // is a reference variable and
  12.     // it gets auto dereferenced. After & and * cancels each other
  13.     // k will hold the value of
  14.     // j which it nothing but address of i
  15.     // Similarly we can also create another reference to the reference variable k
  16.     int *const l = &*k; // A reference to a reference to a reference variable.
  17.     // The variable l will also hold address of i because k holds address of i after
  18.     // & and * cancels each other.
  19.     // so we have seen that all the reference variable will actually holds the same
  20.     // variable address.
  21.     // Now if we increment any one of them the effect will be visible on all the
  22.     // variables.
  23.     // First print original values. The reference variables will have * prefixed because
  24.     // these variables gets automatically dereferenced.
  25.     // The print should be 10,10,10,10
  26.     cout<< i << "," << *j << "," << *k << "," << *l <<endl;
  27.     // increment variable j
  28.     (*j)++;
  29.     // The print should be 11,11,11,11
  30.     cout<< i << "," << *j << "," << *k << "," << *l <<endl;
  31.     // increment variable k
  32.     (*k)++;
  33.     // The print should be 12,12,12,12
  34.     cout<< i << "," << *j << "," << *k << "," << *l <<endl;
  35.     // increment variable l
  36.     (*l)++;
  37.     // The print should be 13,13,13,13
  38.     cout << i << "," << *j << "," << *k << "," << *l <<endl;
  39.     return 0; 
  40. }

    我们通过下面代码可以证明 c++ 的引用不是神马别名,它也会占用内存空间的。


点击(此处)折叠或打开

  1. #include <iostream.h>
  2.     class Test
  3.     {
  4.         int &i; // int *const i;
  5.         int &j; // int *const j;
  6.         int &k; // int *const k;
  7.     };
  8.     int main()
  9.     {
  10.         // This will print 12 i.e. size of 3 pointers
  11.         cout<< "size of class Test = " << sizeof(class Test) <<endl;
  12.         return 0;
  13.     }


结论

我希望这篇文章能把 c++ 引用的所有东东都解释清楚,然而我要指出的是 c++ 标准并没有解释编译器如何实现引用的行为。所以实现取决于编译器,而大多数情况下就是将其实现为一个 const 指针。

引用支持 c++ 虚函数机制的代码


点击(此处)折叠或打开

  1. #include <iostream.h>
  2. class A {
  3.     public:
  4.         virtual void print() { cout<<"A.."<<endl; } 
  5. };
  6. class B : public A {
  7.     public:
  8.         virtual void print() { cout<<"B.."<<endl; } 
  9. };
  10.        
  11. class C : public B {
  12.     public:
  13.         virtual void print() { cout<<"C.."<<endl; }
  14.     };

  15. int main() {
  16.     C c1;
  17.     A &a1 = c1;
  18.     a1.print(); // prints C
  19.     A a2 = c1;
  20.     a2.print(); // prints A
  21.     return 0;
  22. }

上述代码使用引用支持虚函数机制。如果引用仅仅是一个别名,那如何实现虚函数机制,而虚函数机制所需要的动态信息只能通过指针才能实现,所以更加说明引用其实就是一个 const 指针。









阅读(1263) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~