目录(作用):

  1:修饰变量,说明该变量不可以被改变;
  2:修饰指针,分为指向常量的指针和自身是常量的指针
  3:修饰引用,指向常量的引用,用于修饰形参,即避免了拷贝,有避免了函数对值的修改; 
  4:修改成员函数:说明该成员函数内不能修改成员变量。(但是成员变量加上mutable就可以了
 
 
正文:
 
 以下是对各种情况的示例:
#注:1:const修饰的引用cj的值且引用的对象无法修改,但是引用的i是可修改的
#include <iostream> using namespace std; int main() {
int i = ;
const int &cj = i; cout << "cj : " <<cj<< endl;(√) i=;
cout << "cj : " <<cj<< endl;(√) cj=;
cout << "cj : " <<cj<< endl;(×)//引用之后,cj类似于常量,不能做赋值操作,同样的重新引用也不行;只可读的 int a=;
&cj=a; (×) return ;
} 错误提示:
/code/main.cpp: In function ‘int main()’:
/code/main.cpp::: error: assignment of read-only reference ‘cj’
cj=;
^
/code/main.cpp::: error: assignment of read-only reference ‘cj’
cj=a;
^
sandbox> exited with status
1 注:常量引用,本身也要是常量才行:

#include <iostream> using namespace std; int main() {
const int i = ; const int &ck = i; //正确,常量对象绑定到 const引用
cout<< "ck="<<ck<<endl; const int b = ; int &r = b; //错误, return ;
} /code/main.cpp: In function ‘int main()’:
/code/main.cpp::: error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’
int &r = b; //错误,
^
sandbox> exited with status
 注:const 的隐式转换:

#include <iostream> using namespace std; int main() {
double b = 2.14;
const int &a = b;
// 会进行如下转换:
// int temp = b;
// const int &a=temp;
// 所以,给b进行赋值,a可能
cout<<"a="<<a<<endl;
return ;
} 运行结果:
a=
sandbox> exited with status
 注:修饰成员函数_1:

class Date
{
private:
int m_year;
int m_month;
int m_day;
public:
int GetDay(void) const
{
m_day=;
return m_day;//修饰的情况下,不能对成员变量进行修改;
}
}; int main() {
double b = 2.14;
const int &a = b;
// 会进行如下转换:
// int temp = b;
// const int &a=temp;
// 所以,给b进行赋值,a可能
cout<<"a="<<a<<endl;
return ;
} 错误提示:
/code/main.cpp: In member function ‘int Date::GetDay() const’:
/code/main.cpp::: error: assignment of member ‘Date::m_day’ in read-only object
m_day=;
^
sandbox> exited with status 0
 1 注:修饰函数_2

 #include <iostream>

     using namespace std;

 class Date
{
private:
int m_year;
int m_month;
mutable int m_day;//通过被mutable修改的成员变量,就可以被修改了
public:
int GetDay(void) const
{
m_day=;
return m_day;
}
}; // void GetDay(void) const
// {
// return m_day; // } int main() {
double b = 2.14;
const int &a = b;
// 会进行如下转换:
// int temp = b;
// const int &a=temp;
// 所以,给b进行赋值,a可能
cout<<"a="<<a<<endl;
return ;
} 运行结果:
a=
sandbox> exited with status
 注:const修饰的指针
#include <iostream> using namespace std; int main() {
const int* p = NULL;//这两种修饰的是*p指向的值
//int const* p = NULL; int a=;
p=&a;//修改了p指向的地址,任然没有出错
cout<<"*p="<<*p<<endl<<"p="<<p<<endl; int c=;
int* const b = &c;//这两种修饰的是p指向的地址
c=;
*b=c;//修改了b指向的值,任然不会出错
cout<<"*b="<<*b<<endl<<"b="<<b<<endl; b=&a;//这里有问题了,b指向的地址是不能修改的
cout<<"*b="<<*b<<endl<<"b="<<b<<endl;
return ;
} 运行结果:
/code/main.cpp: In function ‘int main()’:
/code/main.cpp::: error: assignment of read-only variable ‘b’
b=&a;
^
sandbox> exited with status
 注:const修饰的引用

#include <iostream> using namespace std; int main() {
int x = ;
const int& y = x;
cout<<"y="<<y<<endl;
x=;
cout<<"y="<<y<<endl; y=;//const修饰的引用是不能够在更改引用指向的对象的
cout<<"y="<<y<<endl;
return ;
} 运行结果:
/code/main.cpp: In function ‘int main()’:
/code/main.cpp::: error: assignment of read-only reference ‘y’
y=;
^
sandbox> exited with status
 

C++中const的特性的更多相关文章

  1. 实例讲述PHP面向对象的特性;;;php中const与define的使用区别

    php中const与define的使用区别 1.const:类成员变量定义,一旦定义且不能改变其值. define:定义全局常量,在任何地方都可以访问.2.define:不能在类中定义,而const可 ...

  2. C++中const 的各种用法

    C++中const 关键字的用法 const修饰变量 const 主要用于把一个对象转换成一个常量,例如: ; size = ; // error: assignment of read-only v ...

  3. (转) C/C++中const关键字详解

    文章转自  http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html 为什么使用const?采用符号常量写出的代码更容易维 ...

  4. 专家解读Linux操作系统内核中的GCC特性

    专家解读Linux操作系统内核中的GCC特性   Linux内核使用GNU Compiler Collection (GCC)套件的几个特殊功能.这些功能包括提供快捷方式和简化以及向编译器提供优化提示 ...

  5. js中const,var,let区别(转载)

    js中const,var,let区别 来源:https://www.cnblogs.com/zzsdream/p/6372729.html 今天第一次遇到const定义的变量,查阅了相关资料整理了这篇 ...

  6. C++中const使用注意要点(一)

    最近看<C++编程思想>发现自己的基础确实不牢固,也想起了以前写代码时也因为const的事情浪费过时间,这里总结下几个要点. 首先说下内部链接和外部链接. 当一个cpp文件在编译时,预处理 ...

  7. C++中const限定符

    const基础 C++中的const,用于定义一个常量,这个常量的值不能被修改.因为const对象一旦创建就不能修改,所以const对象必须初始化.const常量特征仅仅在执行改变其本身的操作时才会发 ...

  8. 1 PHP 5.3中的新特性

    1 PHP 5.3中的新特性 1.1 支持命名空间 (Namespace) 毫无疑问,命名空间是PHP5.3所带来的最重要的新特性. 在PHP5.3中,则只需要指定不同的命名空间即可,命名空间的分隔符 ...

  9. CUDA中关于C++特性的限制

    CUDA中关于C++特性的限制 CUDA官方文档中对C++语言的支持和限制,懒得每次看英文文档,自己尝试翻译一下(没有放lambda表达式的相关内容,太过于复杂,我选择不用).官方文档https:// ...

随机推荐

  1. python3(二十七)property

    """ """ __author__ = 'shaozhiqi' # 绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单, # 但是, ...

  2. Erlang语言之简述及安装

    1. 简述 Erlang在1991年由爱立信公司向用户推出了第一个版本,经过不断的改进完善和发展,在1996年爱立信又为所有的Erlang用户提供了一个非常实用且稳定的OTP软件库并在1998年发布了 ...

  3. 如何使用Swagger-UI在线生成漂亮的接口文档

    一.简单介绍 Swagger是一个实现了OpenAPI(OpenAPI Specification)规范的工具集.OpenAPI是Linux基金会的一个项目,试图通过定义一种用来描述API格式或API ...

  4. windows VMware 安装mac 系统

    0x00 下载链接 首先肯定要有镜像: 链接:https://pan.baidu.com/s/190NBRBwNXVOYRxb6nodHeA 提取码:ahq5 然后还得有这个插件: 链接:https: ...

  5. 【python实现卷积神经网络】全连接层实现

    代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...

  6. 【Java】抽象类、接口

    什么是抽象类? 特点: - 抽象类几乎普通类一样,除了不能实例化 - 不能实例化不代表没有构造器,依然可以声明构造器,便于子类实例化调用 - 具有抽象方法的类,一定是抽象类 abstract 抽象的 ...

  7. 用一个完整的案例讲解Python数据分析的整个流程和基础知识

    先来想一下数据分析的流程,第一步获取数据,因此本节内容就是获取数据以及对数据的基本操作. 1.数据导入 1.1 导入.xlsx文件 要导入一个.xlsx后缀的Excel文件,可以使用pd.read_e ...

  8. stand up meeting 11/20/2015

    3组员 今日工作 工作耗时/h 明日计划 计划耗时/h 冯晓云 将输出string里的翻译合理取分为动名词等各种词性,按约定格式返回,按热度排列,但每一个词性下的解释仍然是由“$$”分词:对于查询词为 ...

  9. Reward 杭电 2647

    Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he ...

  10. 15分钟从零开始搭建支持10w+用户的生产环境(二)

    上一篇文章,把这个架构的起因,和操作系统的选择进行了详细说明. 原文地址:15分钟从零开始搭建支持10w+用户的生产环境(一)   二.数据库的选择 对于一个10W+用户的系统,数据库选择很重要. 一 ...