const用法


<h2><a target=_blank name="t0"></a><strong>1、什么是const? </strong></h2>
常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。(当然,我们可以偷梁换柱进行更新:) 

<h2><a target=_blank name="t1"></a><strong>2、为什么引入const?</strong></h2>const 推出的初始目的,正是为了取代预编译指令,消除它的缺点,同时继承它的优点。 

<h2><a target=_blank name="t2"></a><strong>3、cons有什么主要的作用?</strong></h2>(1)可以定义const常量,具有不可变性。 例如: 
<span style="white-space: pre;">	</span>const int Max=100; int Array[Max]; 
(2)便于进行类型检查,使编译器对处理内容有更多了解,消除了一些隐患。
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code">例如: void f(const int i) { .........} 编译器就会知道i是一个常量,不允许修改。
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code">(3)可以避免意义模糊的数字出现,同样可以很方便地进行参数的调整和修改。 同宏定义一样,可以做到不变则已,一变都变!如(1)中,如果想修改Max的内容,只需要:const int Max=you want;即可!
 (4)可以保护被修饰的东西,防止意外的修改,增强程序的健壮性。 还是上面的例子,如果在函数体内修改了i,编译器就会报错; 例如: <pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code">void f(const int i) { i=10;//error! } 
(5) 为函数重载提供了一个参考。 
<div class="dp-highlighter bg_cpp"><div class="bar"><div class="tools"><strong>[cpp]</strong> <a target=_blank title="view plain" class="ViewSource" href="http://blog.csdn.net/left_la/article/details/7441159#">view plain</a><a target=_blank title="copy" class="CopyToClipboard" href="http://blog.csdn.net/left_la/article/details/7441159#">copy</a><a target=_blank title="print" class="PrintSource" href="http://blog.csdn.net/left_la/article/details/7441159#">print</a><a target=_blank title="?" class="About" href="http://blog.csdn.net/left_la/article/details/7441159#">?</a></div></div><ol class="dp-cpp"><li class="alt"><span><span class="keyword">class</span><span> A { ......   </span></span></li><li><span><span class="keyword">void</span><span> f(</span><span class="datatypes">int</span><span> i) {......} </span><span class="comment">//一个函数 </span><span>  </span></span></li><li class="alt"><span><span class="keyword">void</span><span> f(</span><span class="datatypes">int</span><span> i) </span><span class="keyword">const</span><span> {......} </span><span class="comment">//上一个函数的重载 ...... </span><span>  </span></span></li><li><span>};   </span></li></ol></div><pre class="cpp" style="display: none;" name="code">class A { ...... 
void f(int i) {......} //一个函数 
void f(int i) const {......} //上一个函数的重载 ...... 
}; 
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code">(6) 可以节省空间,避免不必要的内存分配。 例如: 
 
  1. #define PI 3.14159 //常量宏   
  2. const doulbe Pi=3.14159; //此时并未将Pi放入ROM中 ......   
  3. double i=Pi; //此时为Pi分配内存,以后不再分配!   
  4. double I=PI; //编译期间进行宏替换,分配内存   
  5. double j=Pi; //没有内存分配   
  6. double J=PI; //再进行宏替换,又一次分配内存!   
#define PI 3.14159 //常量宏 
const doulbe Pi=3.14159; //此时并未将Pi放入ROM中 ...... 
double i=Pi; //此时为Pi分配内存,以后不再分配! 
double I=PI; //编译期间进行宏替换,分配内存 
double j=Pi; //没有内存分配 
double J=PI; //再进行宏替换,又一次分配内存! 
const定义常量从汇编的角度来看,只是给出了对应的内存地址,而不是象#define一样给出的是立即数,所以,const定义的常量在程序运行过程中只有一份拷贝,而#define定义的常量在内存中有若干个拷贝。 
 
(7) 提高了效率。 编译器通常不为普通const常量分配存储空间,而是将它们保存在符号表中,这使得它成为一个编译期间的常量,没有了存储与读内存的操作,使得它的效率也很高。
 
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code"><h2><a target=_blank name="t3"></a><strong>4、如何使用const?</strong></h2><pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code">(1)修饰一般常量 一般常量是指简单类型的常量。这种常量在定义时,修饰符const可以用在类型说明符前,也可以用在类型说明符后。
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code"> <span style="white-space: pre;">	</span>例如: int const x=2; 或 const int x=2; 
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code">(2)修饰常数组 定义或说明一个常数组可采用如下格式:
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code"><span style="white-space: pre;">	</span>int const a[5]={1, 2, 3, 4, 5}; 
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code"><span style="white-space: pre;">	</span>const int a[5]={1, 2, 3, 4, 5}; 
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code">(3)修饰常对象 常对象是指对象常量,定义格式如下: <span style="white-space: pre;">	</span>
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code"><span style="white-space: pre;">	</span>class A; 
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code"><span style="white-space: pre;">	</span>const A a; 
<span style="white-space: pre;">	</span>A const a; 
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code">定义常对象时,同样要进行初始化,并且该对象不能再被更新,修饰符const可以放在类名后面,也可以放在类名前面。 
(4)修饰常指针 
<span style="white-space: pre;">	</span>const int *A; //const修饰指向的对象,A可变,A指向的对象不可变 
<span style="white-space: pre;">	</span>int const *A; //const修饰指向的对象,A可变,A指向的对象不可变 
<span style="white-space: pre;">	</span>int *const A; //const修饰指针A, A不可变,A指向的对象可变 
<span style="white-space: pre;">	</span>const int *const A;//指针A和A指向的对象都不可变 
(5)修饰常引用 使用const修饰符也可以说明引用,被说明的引用为常引用,该引用所引用的对象不能被更新。
<pre class="reply-text mb10" id="best-answer-content" style="padding: 0px; line-height: 22px; font-family: Arial; font-size: 14px; margin-top: 0px; margin-bottom: 10px; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(255, 252, 246);" name="code"><span style="white-space: pre;">	</span>其定义格式如下: 
<span style="white-space: pre;">	</span>const double & v; 
(6)修饰函数的常参数 const修饰符也可以修饰函数的传递参数,格式如下: 
<span style="white-space: pre;">	</span>void Fun(const int Var); 告诉编译器Var在函数体中的无法改变,从而防止了使用者的一些无意的或错误的修改。
 (7)修饰函数的返回值: const修饰符也可以修饰函数的返回值,是返回值不可被改变,格式如下: 
<span style="white-space: pre;">	</span>const int Fun1(); const MyClass Fun2(); 
(8)修饰类的成员函数: const修饰符也可以修饰类的成员函数,格式如下: 
<div class="dp-highlighter bg_cpp"><div class="bar"><div class="tools"><strong>[cpp]</strong> <a target=_blank title="view plain" class="ViewSource" href="http://blog.csdn.net/left_la/article/details/7441159#">view plain</a><a target=_blank title="copy" class="CopyToClipboard" href="http://blog.csdn.net/left_la/article/details/7441159#">copy</a><a target=_blank title="print" class="PrintSource" href="http://blog.csdn.net/left_la/article/details/7441159#">print</a><a target=_blank title="?" class="About" href="http://blog.csdn.net/left_la/article/details/7441159#">?</a></div></div><ol class="dp-cpp"><li class="alt"><span><span class="keyword">class</span><span> ClassName {   </span></span></li><li><span><span class="keyword">public</span><span>:   </span></span></li><li class="alt"><span><span class="datatypes">int</span><span> Fun() </span><span class="keyword">const</span><span>; .....   </span></span></li><li><span>};   </span></li></ol></div><pre class="cpp" style="display: none;" name="code">class ClassName { 
public: 
int Fun() const; ..... 
}; 
这样,在调用函数Fun时就不能修改类里面的数据 
(9)在另一连接文件中引用const常量 
<span style="white-space: pre;">	</span>extern const int i;//正确的引用 
<span style="white-space: pre;">	</span>extern const int j=10;//错误!常量不可以被再次赋值 另外,还要注意,常量必须初始化! 例如: const int i=5; 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值