简介C++中的const

1、const 引用是指向 const 对象的引用:

const int ival = 1024;
const int &refVal = ival; // ok: both reference and object are const
int &ref2 = ival;  // error: non const reference to a const object

可以读取但不能修改 refVal。同理,用 ival 初始化 ref2 也是不合法的。ref2 是普通的非 const 引用,因此可以用来修改 ref2 指向的对象的值。通过 ref2 对 ival 赋值会导致修改 const 对象的值。为阻止这样的修改,需要规定将普通的引用绑定到 const 对象是不合法的。

const 引用可以初始化为不同类型的对象或者初始化为右值,如字面值常量:

int i = 42;
// legal for const references only
const int &r = 42;
const int &r2 = r + i;

同样的初始化对于非 const 引用却是不合法的,而且会导致编译时错误。

即:非 const 引用只能绑定到与该引用同类型的对象。而const 引用则可以绑定到不同但相关的类型的对象或绑定到右值。

2、每种容器类型定义了一种名为 const_iterator 的类型,该类型只能用于读取容器内元素,但不能改变其值。即使用 const_iterator 类型时,我们可以得到一个迭代器,它自身的值可以改变,但不能用来改变其所指向的元素的值。可以对迭代器进行自增以及使用解引用操作符来读取值,但不能对该元素赋值。

例如,如果text是vector<string> 类型,程序员想要遍历它,输出每个元素,可以这样编写程序:

// use const_iterator because we won't change the elements
for (vector<string>::const_iterator iter = text.begin();iter != text.end(); ++iter)
cout << *iter << endl; // print each element in text

除了是从迭代器读取元素值而不是对它进行赋值之外,这个循环与普通循环相似。由于这里只需要借助迭代器进行读,不需要写,这里把iter 定义为const_iterator 类型。当对const_iterator 类型解引用时,返回的是一个const 值。不允许用const_iterator进行赋值

for (vector<string>::const_iterator iter = text.begin();iter != text.end(); ++ iter)
*iter = " ";   // error: *iter is const

总而言之:使用const_iterator类型时,我们可以得到一个迭代器,它自身的值可以改变,但不能用来改变其所指向的元素的值。可以对迭代器进行自增以及使用解引用操作符来读取值,但不能对该元素赋值。

3、不要把const_iterator对象与const的iterator对象混淆起来。声明一个const迭代器时,必须初始化迭代器。一旦被初始化后,就不能改变它的值:

vector<int> nums(10); // nums is nonconst
const vector<int>::iterator cit = nums.begin();
*cit = 1;        // ok: cit can change its underlying element
++cit;         // error: can't change the value of cit
即:// an iterator that cannot write elements
vector<int>::const_iterator
// an iterator whose value cannot change
const vector<int>::iterator

时间: 2024-08-21 08:16:15

简介C++中的const的相关文章

简介Linux中cp和mv搭配{,}在shell当中的用法

  这篇文章主要介绍了简介Linux中cp和mv搭配{,}在shell当中的用法,作者举了四个这样的大括号扩展示例,需要的朋友可以参考下 经常会在博客或者论坛看到类似下面的命令 大括号扩展 Brace expansion { } shell 作用 ? 1 cp /etc/httpd/httpd.{,.bakup} 或者是 ? 1 mv resume{z,}.doc 那么,在uinx / linux shell命令中是什么意思?起什么作用呢? { } 并没有什么实际的含义,但是却可以作为Brace

简介Redis中的showlog功能

  这篇文章主要介绍了简介Redis中的showlog功能,作者同时对比了DEL命令的性能,需要的朋友可以参考下 Redis 有一个实用的slowlog功能,正如你可以猜到的,可以让你检查运行缓慢的查询. Slowlog 将会记录运行时间超过Y微秒的最后X条查询. X 和 Y 可以在 redis.conf 或者在运行时通过 CONFIG 命令: 代码如下: CONFIG SET slowlog-log-slower-than 5000 CONFIG SET slowlog-max-len 25

简介JavaScript中用于处理正切的Math.tan()方法

  这篇文章主要介绍了简介JavaScript中用于处理正切的Math.tan()方法,是JS入门学习中的基础知识,需要的朋友可以参考下 这个方法返回一个数字的正切值.tan 方法返回表示的角度的正切值. 语法 ? 1 Math.tan( x ) ; 下面是参数的详细信息: x: 一个数字,表示以角度表示弧度 返回值: 返回一个数字的正切值 例子: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <html> <head&g

c++-initializer_list中的const auto&amp;amp;amp; 问题

问题描述 initializer_list中的const auto& 问题 void error_msg(ErrCode e, initializer_list<string> il) { cout << e.msg() << ":"; for (const auto &elem : il) cout << elem << " "; cout << endl; } initial

c++-C++类中函数+const是什么意思,

问题描述 C++类中函数+const是什么意思, class twopoint{ protected: double x,y; public: twopoint(double i,double j):x(i),y(j){ } void setX(double NewX) { x=NewX; } void setY(double NewY){ y=NewY; } double getX()const{return x;} double getY()const{return y;} }; 最后两行的

c++中关于const的应用

问题描述 c++中关于const的应用 用变量初始化const引用,然变量形参拥有只读性 用字面量初始化const引用,额外的分配内存空间这个问题怎么解决? 解决方案 额外的分配了内存空间,只能等释放的时候一起释放吧.想不到其他的办法.你可以参考下const用法:http://blog.csdn.net/ckh2007/article/details/5148921 解决方案二: 今天我给大家详细地讲一讲C++中const关键字的应用1> 指向const对象的指针?格式:const int *c

C++中的const和constexpr详解_C 语言

C++中的const可用于修饰变量.函数,且在不同的地方有着不同的含义,现总结如下. const的语义 C++中的const的目的是通过编译器来保证对象的常量性,强制编译器将所有可能违背const对象的常量性的操作都视为error. 对象的常量性可以分为两种:物理常量性(即每个bit都不可改变)和逻辑常量性(即对象的表现保持不变).C++中采用的是物理常量性,例如下面的例子: struct A { int *ptr; }; int k = 5, r = 6; const A a = {&k};

浅谈Java中的final关键字与C#中的const, readonly关键字_java

在编程语言中都有某种方式,告知编译器一块数据是恒定不变的.有两个需求 1. 一个永不改变的编译器常量 2. 一个在运行时被初始化的值,而这个值不会被改变 在Java中,使用final修饰变量实现这两个需求 <pre name="code" class="java">//编译器常量 private final int valueOne = 9; private static final int VALUE_TWO = 99; public static f

c++的问题-C++类中定义 const static 成员变量为啥在VC++6.0中 编译不过

问题描述 C++类中定义 const static 成员变量为啥在VC++6.0中 编译不过 class GradeBook { public: //constant -- number of students who took the test const static int students = 0; //constructor initialize course name and array of grades GradeBook( string, const int [] ); voi