第一眼见到explicit和volatile可能会一愣一愣的觉得可能是c11或者c14新加的标识符。

其实不是这样,volatile和const两个关键字在C语言的第二个版本KR C的时候就被加入了C标准,他们是两个相对的关键字

const 修饰符表示这是一个常量类型,这个变量的值不会被程序改变

volatile 修饰符表示这个变量可能被编译器以外的行为(譬如内联汇编程序)改变。

修饰常量变量只要和类型紧挨着就可以

int const a = ;
const int a = ;

修饰指针时以*号为分界符号

#include <iostream>
#include <iostream> int main() {
int a = ;
int b = ; const int *p1 = &a;
int const *p2 = &a;
int *const p3 = &a;
const int *const p4 = &a;
int const *const p5 = &a; printf("&a = %X\n", &a);
printf("&b = %X\n", &b); printf("p1 = 0x%X , *p1 = %d\n", p1, *p1);
printf("const int *p1 = &a; \n (*p1) = b;通过p修改a的值\n");
// (*p1) = b;
p1 = &b;
printf("p1 = 0x%X , *p1 = %d\n", p1, *p1);
printf("p2 = 0x%X , *p2 = %d\n", p2, *p2);
printf("int const *p2 = &a; \n (*p2) = b;通过p修改a的值\n");
// (*p2) = b;
p2 = &b;
printf("p2 = 0x%X , *p2 = %d\n", p2, *p2);
printf("p3 = 0x%X , *p3 = %d\n", p3, *p3);
printf("int *const p3 = &a; \n p3 = &b;修改p的指向\n");
// p3 = &b;
(*p3) = b;
printf("p3 = 0x%X , *p3 = %d\n", p3, *p3);
printf("p4 = 0x%X , *p4 = %d\n", p4, *p4);
printf("const int *const p4 = &a; \n 二者都不能修改\n");
// (*p4) = &b;
// p4 = &b;
printf("p4 = 0x%X , *p4 = %d\n", p4, *p4);
printf("p5 = 0x%X , *p5 = %d\n", p5, *p5);
printf("int const *const p5 = &a; \n 二者都不能修改\n");
// (*p5) = &b;
// p5 = &b;
printf("p5 = 0x%X , *p5 = %d\n", p5, *p5);
return ;
}

gcc报错结果

J:\SITP\alg\main.cpp: In function 'int main()':
J:\SITP\alg\main.cpp::: error: assignment of read-only location '* p1'
(*p1) = b;
^
J:\SITP\alg\main.cpp::: error: assignment of read-only location '* p2'
(*p2) = b;
^
J:\SITP\alg\main.cpp::: error: assignment of read-only variable 'p3'
p3 = &b;
^
J:\SITP\alg\main.cpp::: error: assignment of read-only location '*(const int*)p4'
(*p4) = &b;
^
J:\SITP\alg\main.cpp::: error: invalid conversion from 'int*' to 'int' [-fpermissive]
J:\SITP\alg\main.cpp::: error: assignment of read-only variable 'p4'
p4 = &b;
^
J:\SITP\alg\main.cpp::: error: assignment of read-only location '*(const int*)p5'
(*p5) = &b;
^
J:\SITP\alg\main.cpp::: error: invalid conversion from 'int*' to 'int' [-fpermissive]
J:\SITP\alg\main.cpp::: error: assignment of read-only variable 'p5'
p5 = &b;
^
mingw32-make.exe[]: *** [CMakeFiles/alg.dir/main.cpp.obj] Error
mingw32-make.exe[]: *** [CMakeFiles/alg.dir/all] Error
mingw32-make.exe[]: *** [CMakeFiles/alg.dir/rule] Error
CMakeFiles\alg.dir\build.make:: recipe for target 'CMakeFiles/alg.dir/main.cpp.obj' failed
CMakeFiles\Makefile2:: recipe for target 'CMakeFiles/alg.dir/all' failed
CMakeFiles\Makefile2:: recipe for target 'CMakeFiles/alg.dir/rule' failed
mingw32-make.exe: *** [alg] Error
Makefile:: recipe for target 'alg' failed

将非法的行注释掉之后可以看到允许修改的内容

J:\SITP\alg\cmake-build-debug\alg.exe
&a = 72FE24
&b = 72FE20
p1 = 0x72FE24 , *p1 =
const int *p1 = &a;
(*p1) = b;通过p修改a的值
p1 = 0x72FE20 , *p1 =
p2 = 0x72FE24 , *p2 =
int const *p2 = &a;
(*p2) = b;通过p修改a的值
p2 = 0x72FE20 , *p2 =
p3 = 0x72FE24 , *p3 =
int *const p3 = &a;
p3 = &b;修改p的指向
p3 = 0x72FE24 , *p3 =
p4 = 0x72FE24 , *p4 =
const int *const p4 = &a;
二者都不能修改
p4 = 0x72FE24 , *p4 =
p5 = 0x72FE24 , *p5 =
int const *const p5 = &a;
二者都不能修改
p5 = 0x72FE24 , *p5 = Process finished with exit code

C++ - explicit和volatile/const的内容的更多相关文章

  1. c++const小结

    C++const简单整理,本文的首次是在博客园发布的,如有错误,欢迎大家指正 博客园链接:http://www.cnblogs.com/Forever-Kenlen-Ja/p/3776991.html ...

  2. C++ const用法小结 (欢迎大家拍砖)

    C++const 关键字小结 const 是constant的缩写,本意是不变的,不易改变的意思. const 在C++中是用来修饰内置类型变量,自定义对象,成员函数,返回值,函数参数. 一.cons ...

  3. __I、__O 、__IO volatile是什么?怎么用? .

    这是ST库里面的宏定义,定义如下: #define __I volatile const /*!< defines 'read only' permissions */ #define __O ...

  4. c语言,volatile

          一.意义: 该关键字的意义就是表示定义的变量值随时都会改变,必须从变量的地址处读取值,所以只有这个变量在使用过程中可能被改变(比如中断程序),就需要用这个关键字说明. )volatile, ...

  5. C/C++中const关键字的用法及其与宏定义的比较

    1.const关键字的性质 简单来说:const关键字修饰的变量具有常属性. 即它所修饰的变量不能被修改. 2.修饰局部变量 ; ; 这两种写法是等价的,都是表示变量的值不能被改变,需要注意的是,用c ...

  6. 1.C和C++区别,以及const分析

    从本章起开始从0学习C++,本章主要内容: 1)C和C++的基本区别 2)C和C++的const区别 1.C++和C区别 1.1 C++更强调语言的实用性,所有变量都可以在需要时再定义 比如: ;i& ...

  7. C/C++中const关键字的用法及其与宏常量的比较

    1.const关键字的性质 简单来说:const关键字修饰的变量具有常属性. 即它所修饰的变量不能被修改. 2.修饰局部变量 ; ; 这两种写法是等价的,都是表示变量的值不能被改变,需要注意的是,用c ...

  8. 10.C++-构造函数初始化列表、类const成员、对象构造顺序、析构函数

    首先回忆下,以前学的const 单独使用const修饰变量时,是定义的常量,比如:const int i=1; 使用volatile const修饰变量时,定义的是只读变量 使用const & ...

  9. volatile的陷阱

         对于volatile关键字,大部分C语言的教程都是一笔带过,并没有做太深入的分析,所以这里简单的整理了一些 关于volatile的使用注意事项.实际上从语法上来看volatile和const ...

随机推荐

  1. distinct top執行順序

    select distinct top 3 from table; 先distinct后top

  2. 索引与like优化

    未建索引 mysql> alter table modulestatus drop index imei;Query OK, 457922 rows affected (4.29 sec)Rec ...

  3. fiddler抓web请求

    原理 fiddler抓包原理 fiddler 调试器注册到操作系统因特网服务中,系统所有的网络请求都会走fiddler的代理,所以fiddler才能抓包. Debug traffic from any ...

  4. SqlDataHelper

    using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...

  5. LayDate 时间选择插件的使用介绍 (低版本1.0好像是)

    <span style="font-size:18px;"><!doctype html> <html> <head> <me ...

  6. Codeforces 679B. Barnicle 模拟

    B. Barnicle time limit per test: 1 second memory limit per test :256 megabytes input: standard input ...

  7. Telnet 安装

    Telnet 安装 一.Telnet 安装 (1) 登录目标主机检测 telnet 服务是否正常 [root@localhost ~]# telnet localhost -bash: telnet: ...

  8. 连接数据库-stone

    # -*- coding:utf-8 -*- import pymysql class mysql: def __init__(self, host, port, dbuser, dbpwd, dbn ...

  9. with as 创建临时表,解决union all多个时出现内存出错的问题

    with t(content_id, emp_id) as (VALUES('002', 416),('003', 416))SELECT * FROM t;

  10. myeclipse svn 插件去除已经保存的密码方法

    myeclipse svn 插件去除已经保存的密码方法   删除掉C:\Documents and Settings\hao\Application Data\Subversion\auth\svn. ...