const type& 与 type& 的区别

const type& 与 type& 是C/C++编程中容易混淆的两个知识点,现在以 cont int& 与 int& 为例讲解:

1.int& 讲解

   int a = 10;

   int& b = a;

   a的值可以通过a改变,也可以通过b改变

2.const int& 讲解

   int a = 10;

   const int& b = a;

   a的值只能通过a改变,不能通过b改变

3.const int a = value 的引用只能为 const int b& = a

   因为a的值是无法改变的,所以其引用也无法改变a的值,所以只能为常引用

   

 

 

 

posted @ 2014-03-25 21:19  瓦尔登湖畔的小木屋  阅读(1155)  评论(0编辑  收藏  举报