C++/C 重要的知识点 (一) const的作用

 C++有操作内存的能力,但是这也增加了出错的几率。在增加程序的高效和编程的灵活性的同时,我们也要特别的关注程序的健康和稳定。

    有些知识点,但在编程过程中容易被忽略,这种忽略一般是因为坏的编程习惯,并不严谨的编程模式,对于知识点的一知半解。要形成好的编程习惯,写出高质量的代码就必须对一些知识点有清楚的认识。

    下面是我在项目中总结的一些知识点。

 

(一)const的作用

    首先,const修饰的变量代表这个变量分配好内存后就应该立刻初始化,初始化以后这个变量的值就不能改变了。这个我们拿const修饰的的常变量和宏相比。

     1.前者是变量会做格式的检查,宏不会做格式检查,所以前者相对安全,减少出错的可能。

      2.程序时出错时调试的时候,宏通常只会显示具体的值而不会显示宏的名称,这样就增加了定位问题的难度。但是const修饰的常变量则和其他的变量一样会显示出错的变量名,定位问题相对的容易。

 

   其次,我们用一个短小的程序说明const在函数中的作用。

<a target=_blank id="L1" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank id="L35" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank id="L36" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank id="L37" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank id="L38" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank id="L39" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
<a target=_blank id="L40" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;"> 40</a>
<a target=_blank id="L41" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;"> 41</a>
<a target=_blank id="L42" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L42" rel="#L42" style="color: rgb(102, 102, 102); text-decoration: none;"> 42</a>
<a target=_blank id="L43" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L43" rel="#L43" style="color: rgb(102, 102, 102); text-decoration: none;"> 43</a>
<a target=_blank id="L44" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L44" rel="#L44" style="color: rgb(102, 102, 102); text-decoration: none;"> 44</a>
<a target=_blank id="L45" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L45" rel="#L45" style="color: rgb(102, 102, 102); text-decoration: none;"> 45</a>
<a target=_blank id="L46" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L46" rel="#L46" style="color: rgb(102, 102, 102); text-decoration: none;"> 46</a>
<a target=_blank id="L47" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L47" rel="#L47" style="color: rgb(102, 102, 102); text-decoration: none;"> 47</a>
<a target=_blank id="L48" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L48" rel="#L48" style="color: rgb(102, 102, 102); text-decoration: none;"> 48</a>
<a target=_blank id="L49" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L49" rel="#L49" style="color: rgb(102, 102, 102); text-decoration: none;"> 49</a>
<a target=_blank id="L50" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L50" rel="#L50" style="color: rgb(102, 102, 102); text-decoration: none;"> 50</a>
<a target=_blank id="L51" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L51" rel="#L51" style="color: rgb(102, 102, 102); text-decoration: none;"> 51</a>
<a target=_blank id="L52" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L52" rel="#L52" style="color: rgb(102, 102, 102); text-decoration: none;"> 52</a>
<a target=_blank id="L53" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L53" rel="#L53" style="color: rgb(102, 102, 102); text-decoration: none;"> 53</a>
<a target=_blank id="L54" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L54" rel="#L54" style="color: rgb(102, 102, 102); text-decoration: none;"> 54</a>
<a target=_blank id="L55" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L55" rel="#L55" style="color: rgb(102, 102, 102); text-decoration: none;"> 55</a>
<a target=_blank id="L56" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L56" rel="#L56" style="color: rgb(102, 102, 102); text-decoration: none;"> 56</a>
<a target=_blank id="L57" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L57" rel="#L57" style="color: rgb(102, 102, 102); text-decoration: none;"> 57</a>
<a target=_blank id="L58" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L58" rel="#L58" style="color: rgb(102, 102, 102); text-decoration: none;"> 58</a>
<a target=_blank id="L59" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L59" rel="#L59" style="color: rgb(102, 102, 102); text-decoration: none;"> 59</a>
<a target=_blank id="L60" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L60" rel="#L60" style="color: rgb(102, 102, 102); text-decoration: none;"> 60</a>
<a target=_blank id="L61" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L61" rel="#L61" style="color: rgb(102, 102, 102); text-decoration: none;"> 61</a>
<a target=_blank id="L62" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L62" rel="#L62" style="color: rgb(102, 102, 102); text-decoration: none;"> 62</a>
<a target=_blank id="L63" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L63" rel="#L63" style="color: rgb(102, 102, 102); text-decoration: none;"> 63</a>
<a target=_blank id="L64" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L64" rel="#L64" style="color: rgb(102, 102, 102); text-decoration: none;"> 64</a>
<a target=_blank id="L65" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L65" rel="#L65" style="color: rgb(102, 102, 102); text-decoration: none;"> 65</a>
<a target=_blank id="L66" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L66" rel="#L66" style="color: rgb(102, 102, 102); text-decoration: none;"> 66</a>
<a target=_blank id="L67" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L67" rel="#L67" style="color: rgb(102, 102, 102); text-decoration: none;"> 67</a>
<a target=_blank id="L68" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L68" rel="#L68" style="color: rgb(102, 102, 102); text-decoration: none;"> 68</a>
<a target=_blank id="L69" href="http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307#L69" rel="#L69" style="color: rgb(102, 102, 102); text-decoration: none;"> 69</a>
           
#include <string>
using std :: string ;
class CPerson
{
public:
enum Sexual { man , woman other };
CPerson ( Sexual sex );
~ CPerson (){};
void setName ( const string & name ); //1.const代表的是不会改变函数调用的参数(引用)的值
void printData () const ; //2.const代表的是此函数不会改变任何一个成员变量的值
const string & getName (); //3.const代表返回的引用不能被改变 ,但是并不能返回一个临时的变量
private:
string m_strName ;
const Sexual m_enSex ;
public: // 将变量声明为public并不合适
int m_iAge ;
float m_iWeight ;
float m_iHight ;
};
#include "A0624.h"
#include <iostream>
CPerson :: CPerson ( Sexual sex ) : m_enSex ( sex ){}
// CPerson::CPerson(Sexual sex){ m_enSex = sex; } //ERROR “CPerson::m_enSex” const修饰的成员变量: 必须在构造函数基/成员初始值设定项列表中初始化
void CPerson :: setName ( const string & name )
{
m_strName = name ;
}
const string & CPerson :: getName ()
{
return m_strName ;
}
void CPerson :: printData () const //不仅声明中要带const,在定义的时候也要带const
{
std :: cout << "the name is " << m_strName << std :: endl ;
std :: cout << "the age is " << m_iAge << std :: endl ;
std :: cout << "the hight is " << m_iHight << std :: endl ;
std :: cout << "the weight is " << m_iWeight << std :: endl ;
std :: cout << "the person is " << (( m_enSex == Sexual :: man ) ? "man" : "woman" ) << std :: endl ;
}
int main ( void )
{
CPerson person ( CPerson :: Sexual :: man );
std :: string name ( "BinJey" );
person . setName ( name ); // name的值不会被改变
person . m_iAge = 20 ;
person . m_iHight = ( float ) 1.89 ;
person . m_iWeight = 178 ;
const std :: string & sName = person . getName ();
// sName = "change";
// ERRO C2678: 二进制“=”: 没有找到接受“const std::string”类型的左操作数的运算符(或没有可接受的转换)
person . printData ();
return 0 ;
}
 来自CODE的代码片
const.cpp
 
  1.const修饰参数引用,代表的是不会改变函数调用的参数(引用)的值
  2.const放在声明和定义的后面,代表的是此函数不会改变任何一个成员变量的值
  3.const修饰返回的引用,代表返回的引用不能被改变 (函数不能返回一个临时的变量作为引用,引用的本质是地址)


http://blog.youkuaiyun.com/v_fangfang_v/article/details/34144307
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值