zoukankan      html  css  js  c++  java
  • Duanxx的C++学习: const指针具体解释

     Const指的是一个编译时的常量。

             keywordconst使得代码能够确定一个变量能否够被改动。

    使用了const后,能够防止对变量或者指针的改动;更重要的是,const的引用能够防止对所引用的对象的改动

     

    一般来说,在C语言中,对于一些常量的定义,我习惯性的使用define,而在C++中则最好改为使用const。

     

    对于嵌入式程序而言,const的使用则是相当的微妙的,被const修饰后,其变量是存放在ROM中的,这一点非常重要。

     

    关于Const的指针的使用,文字讲解没有意义,直接參见以下的代码及凝视:

    #include <iostream>
    // const
    // - a compile time constraint that an object can not be modified
    
    int main()
    {
    	const int i = 1;
    	int a = 0;
    	const int *p1 = &i;		///< data is const ,but pointer is not
    
    	int* const p2 = &a;	    ///< pointer p2 itself is const ,but the data p2 point to is not const
    	//int* const p3 = &i;     ///< illegal , cannot convert from 'const int *' to 'int *const '
    	int const* p4 = p1;      ///< if const is on the left of *,data is const
    						///< if cosnt is on the right of *,pointer is const
    
    	const int* const p5 = &i; ///<the data and the pointer are both const
    
    	return 0;
    }
    


  • 相关阅读:
    jQuery的优点——(一)
    js学习之函数表达式及闭包
    js的面向对象的程序设计之理解继承
    js之引用类型
    js的基本概念详解
    有关js的变量、作用域和内存问题
    js和HTML结合(补充知识:如何防止文件缓存的js代码)
    富豪凯匹配串
    2018 ccpc final
    蒟蒻的个人第一篇博客
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4033215.html
Copyright © 2011-2022 走看看