#include <iostream>
using std::cout;
using std::endl;
int main()
{
int i = 5;
const int ic =10;
const int *pic = &i;//从右往左定义“pic 是一个指向int类型的、被定义成const对象的指针”
i = i+1;
pic = pic + 1;
// *pic = 2; Error *pic 不能做左值
const int *pic_1 = ⁣
pic_1 = pic_1 + 1;
const int * const pic_2 = ⁣ //pic_2 是一个指向const int类型的、被定义成const对象的指针
i = i + 1;
// pic_2 = pic_2 + 1;// error assignment of read-only variable 'pic_3'
// *pic_2 = 2; //Error *pic_2 不能做左值
const int * const pic_3 = &i;
i = i+1;
// pic_3 = pic_3 + 1; error assignment of read-only variable 'pic_3'
//*pic_3 = 2; //Error *pic_2 不能做左值
// int * const cpi = ⁣ // cpi是一个指向 const int类型,被定义为非const对象的指针
int * const cpi_1 = &i; //cpi_1是一个指向const int 类型,被定义为非const对象的指针
*cpi_1 = 10;
// cpi = cpi + 1;
return 0;
} //End of main
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int ival = 0;
const int ival3 = 1;
const int &rval1 = 1.01;
int &rval2 = ival;
int * const &rval3 = &ival;
int *pi = &ival;
int * &rval4 = pi;
int &rval5 = *pi;
//int &* prval1 = pi;//error: cannot declare pointer to 'int&'
int * & prval1 = pi;//right
const int & ival2 = 1;
//const int * & prval2 = &ival3;//error: cannot bind non-const lvalue reference of type 'const int*&' to an rvalue of type 'const int*'
const int * const & prval2 = &ival3;
//int * & prval3 = &ival;//error: cannot bind non-const lvalue reference of type 'int*&' to an rvalue of type 'int*'
const int * & prval3 = &ival;//error: cannot bind non-const lvalue reference of type 'int*&' to an rvalue of type 'int*'
//const int * const & prval3 = &ival1;
return 0;
} //End of main
分享到:
相关推荐
C++中const关键字详解
C/C++ 嵌入式 一些关键字: volatile关键字 Const关键字 static关键字 mutable 关键字
关于C/C++中的const关键字的用法,你了解多少呢?
现在读到了const关键字的用法,书上面讲解的时候并没有给出完整的实例,只是理论的讲解了一些知识,后来在网上面搜了关于const的关键字的讲解,但是搜到的内容和书上面讲解的内容有着非常让人疑惑的东西,看看下面的...
关于C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性
一 const基础;二 const的初始化;三 作为参数和返回值的const修饰符;四 类成员函数中const的使用;五 使用const的一些建议。
在 C++ 编程中,使用 const 关键字可以提高代码的可读性、可维护性和安全性。本文将详细介绍 EFFECTIVE C++ 的第三条款:尽量使用 const 思维导图。 一、const 关键字的作用 -------------------- const 关键字是 ...
C++中const关键字详解
const用法详解与示例代码
关键字const用来定义常量,如果一个变量被const修饰,那么它的值就不能再被改变,我想一定有人有这样的疑问,C语言中不是有#define吗,干嘛还要用const呢,我想事物的存在一定有它自己的道理,所以说const的存在一定...
游戏效率优化(2) 使用const关键字1
Const 关键字在 C++ 语言中的应用和优化 Const 关键字是 C++ 语言中一个非常重要的关键字,它可以用来修饰变量、函数参数、函数返回值等,以确保程序的健壮性和可靠性。 Const 关键字的主要作用是防止意外的变动,...
另外,在观看别人代码的时候,清晰理解const所起的作用,对理解对方的程序也有一些帮助。 define与const的区别 1.define作用在预处理时,是简单地字符替换 2. const作用在编译时,具有类型检查的功能 3. const必须...
const关键字 常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。不管出现在任何上下文都是为这个目的而服务的。 const使用方法 定义const对象 const修饰符可以把对象转变成常数对象...
本文档详细介绍了C++中关键字const的用法,并给出了具体的示例来帮助读者理解
主要介绍了深入探讨C#中的const、readonly关键字,本文可以帮助你深刻理解这两个关键字,而且是面试中最可能面试到的问题哦,需要的朋友可以参考下
介绍了C中关键字const的用法和使用区别
对于CONST关键字的使用说明及总结,希望对你有用
主要介绍了php中static和const关键字用法,结合实例形式分析了static和const关键字的功能、使用方法与相关注意事项,需要的朋友可以参考下