知识回顾:C语言中 const

  const 修饰的变量是 只读的,本质上还是变量;(只读变量:可以通过指针修改只读变量中的值)

  const 修饰的局部变量在栈上分配空间;

  const 修饰的全局变量在只读存储区分配空间;

  const 只在编译期间生效,在运行期间无效;

  const 修饰的变量不是真的常量,它只是告诉编译器该变量不能出现在赋值符号的左边;

 #include <stdio.h>

 int main()
{
const int c = ; // c 是只读变量
int* p = (int*)&c; *p = ; printf("c = %d\n", c); // c = 5 return ;
}
// 编译器 gcc

c语言中 const



c++ 中 const

1. const 常量

  1)const 修饰的变量 是一个真正的常量;

    这是由于c++中使用的符号表机制,当使用一个字面量初始化const变量时,就会将该字面量放入符号表中;在编译过程中,若发现使用该变量,就会将这个变量替换为符号表中值;

  2)在编译过程中,若发生以下情况,则可能会给对应的常量分配存储空间;

    1、当 const 常量为全局时,并且需要在其它文件中使用;(extern)

    2、当使用 & 操作符对 const 常量取地址;

       注:c++ 编译器虽然可能为 const 常量分配存储空间,但不会使用其存储空间中的值;

 #include <stdio.h>

 int main()
{
const int c = ; // c 是常量
int* p = (int*)&c; *p = ; printf("c = %d\n", c); // c = 10 return ;
} // 编译器 g++

const 常量

    

  3)const 常量 与 宏定义

    相同点:

      c++中 const 常量 ≈ 宏定义;( const int a = 10; ≈ #define a 10 )

    不同点:

      const 是由编译器处理,编译器对 const 有 类型检查 和 作用域检查;

      宏定义 是由预处理器处理,只是单纯的文本替换;

 #include <stdio.h>

 void f()
{
#define a 3
const int b = ;
} void g()
{
printf("a = %d\n", a);
//printf("b = %d\n", b); // err
} int main()
{
const int A = ;
const int B = ;
int array[A + B] = {};
int i = ; for(i=; i<(A + B); i++)
{
printf("array[%d] = %d\n", i, array[i]);
} f();
g(); return ;
}

const常量 与 宏定义


  结论:--- 经典问题解析一

    1)const 常量的判断方法:

      1、只有用 字面量初始化的 const 常量才会进入符号表;

      2、使用其它变量初始化的 const 常量仍然是 只读变量;

      3、被 volatile 修饰的 const 常量不会进入符号表;

    2)const 引用类型 与 初始化变量的类型

      相同:初始化变量成为只读变量;

      不同:生成一个新的只读变量;

 #include <stdio.h>

 int main()
{
const int x = ; // 只有用 字面量 初始化的const常量才进入符号表--- 此处x进入符号表,x是常量
const int& rx = x; // 编译器分配给常量x的内存地址的别名为 rx--- rx是只读变量 int& nrx = const_cast<int&>(rx); // 去除只读变量rx的只读属性 --- nrx nrx = ; printf("x = %d\n", x); //
printf("rx = %d\n", rx); //
printf("nrx = %d\n", nrx); //
printf("&x = %p\n", &x); // 0x7ffd8aed8f90
printf("&rx = %p\n", &rx); // 0x7ffd8aed8f90
printf("&nrx = %p\n", &nrx); // 0x7ffd8aed8f90 volatile const int y = ; // volatile 修饰的const常量不会进入符号表---y是只读变量
int* p = const_cast<int*>(&y); // 去除y的只读属性 *p = ; printf("y = %d\n", y); //
printf("&y = %p\n", &y);// 0x7ffd8aed8f94
printf("p = %p\n", p); // 0x7ffd8aed8f94 const int z = y; // 用 其它变量 初始化const常量,仍是只读变量 --- z是只读变量 p = const_cast<int*>(&z); // 去除z的只读属性 *p = ; printf("z = %d\n", z); //
printf("&z = %p\n", &z);// 0x7ffd8aed8f98
printf("p = %p\n", p); // 0x7ffd8aed8f98 char c = 'c';
char& rc = c;
const int& trc = c; //const引用类型 与 初始化变量的类型不一致,则生成新的只读变量 --- trc为新的只读变量 printf("c = %c\n", c); // c
printf("rc = %c\n", rc); // c
printf("trc = %c\n", trc); // c rc = 'a'; printf("c = %c\n", c); // a
printf("rc = %c\n", rc); // a
printf("trc = %c\n", trc); // c return ;
}

1)2)结论的代码展示

    3) 指针常量 Type * const pt (引用的本质)

      c++中没有引用数组的概念;(因为数组在内存中是一段连续的存储空间,然而引用数据并不满足这个要求)

2 类中的 const(const 对象  和 const 成员函数)--- 经典问题解析二

  const 对象的特点:

    1)const修饰的对象为只读对象;

    2)只读对象的成员变量不可以修改;

    3)只读对象是编译阶段的概念,运行时无效;

    注:const 对象 不推荐使用;

  const 成员函数的特点:

    1)只读对象只能调用const成员函数;

    2)const成员函数 只能 调用const成员函数;

    3)const成员函数 不能 直接修改成员变量的值;(const 成员函数 使 this指针所指向的对象具有了只读属性)

    注:const 成员函数 在 声明与定义的时候必须都有 const;

 #include <stdio.h>

 class Test
{
int mi;
public:
Test(int i);
Test(const Test& t);
int getMi() const;
void print();
}; Test::Test(int i)
{
mi = i;
} Test::Test(const Test& t)
{ } int Test::getMi() const
{
//mi = 0; // error // const成员函数 不能 直接修改成员变量的值
//print();// error // const成员函数 只能 调用const成员函数
return mi;
} void Test::print()
{
printf("v = %d\n", mi);
} int main()
{
/*
* 1 const可以修饰类的对象
* 2 const修饰的对象为只读对象
* 3 只读对象的成员变量不可以修改
* 4 只读对象是编译阶段的概念,运行时无效
*/
/*
* 1 只读对象只能调用const成员函数
* 2 const成员函数 只能 调用const成员函数
* 3 const成员函数 不能 直接修改成员变量的值
*/
const Test t();
printf("v = %d\n", t.getMi()); // 只读对象只能调用const成员函数 return ;
}

const 对象 和 const 成员函数

    

c++中的 const 关键字的更多相关文章

  1. C++中的const关键字

    http://blog.csdn.net/eric_jo/article/details/4138548 C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方 ...

  2. C++ 类中的const关键字

    //类中的const关键字 #include<iostream> using namespace std; class Point{ public: //这个const关键字本质上修饰的是 ...

  3. C++中的const关键字的用法

    1.const用于修饰普通变量,表示常量,不建议修改,某种程度上不允许修改(其实也是可以修改的) 指针常量 :指针(指向的变量的值)自身是一个常量,说明不能改变自身的指向  int* const p= ...

  4. C++中的const关键字学习笔记

    一.const引用 1. 例子一 #include <iostream> using namespace std; class sp { public: sp() {cout<< ...

  5. c++中的const关键字的理解

    看effective c++第二版推荐使用const,少用define.今天才发现发现这远远不够. #define定义的常量在预处理替换,debug的时候无法打印宏的,这种常量设置是有缺陷的, con ...

  6. C++中const关键字的使用总结

    C++中使用const关键字来修饰常量,下面从两个方面总结:变量和成员函数. 变量:const可以修饰普通变量.指针(数组)和结构体. 1.const修饰普通变量是最简单的情形.这样的用法多为在程序中 ...

  7. c++ --> const关键字总结

    const关键字总结 C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性.const 是C++中常用的类型修饰符,常类型是指使用类型修饰符const说明的类型,常类型的变 ...

  8. 【3】学习C++之const关键字的使用

    在C++中,const关键字是控制变量是否可以变化的,是否能够用好const关键字是区别小白和大佬的重要指标(大雾). 1.const与基本数据类型 ; //a是变量,a的值可以在后续操作中进行更改. ...

  9. const关键字到底该怎么用

    原文地址:https://www.yanbinghu.com/2019/01/28/7442.html 前言 我们都知道使用const关键字限定一个变量为只读,但它是真正意义上的只读吗?实际中又该如何 ...

随机推荐

  1. centos6.9防火墙设置

    1.输入:cat /etc/issue   查看版本 2. service命令开启以及关闭防火墙为即时生效,下次重启机器的时候会自动复原. 查看防火墙状态:service iptables statu ...

  2. 三十八、LNMP潮流组合搭建

    一.安装mysql 数据库 1.1  mysql数据库安装的三种方法: 1)编译安装,在lamp经典组合安装是5.1版本,是configure,make,make install,这里如果是5.5版本 ...

  3. mysql模糊匹配like及批量替换replace

    1.mysql 模糊匹配 like 与 not like 用法 : SELECT * FROM `user` where `nickname` LIKE '%测试%' SELECT * FROM `u ...

  4. set|lambda|reduce

    #!/usr/bin/python a=set([i for i in range(4,8)]) b=set([i for i in range(5,12)]) c= sorted(a & b ...

  5. IPC之——信号量集

    信号量集用于对存在竞争的资源加锁 1.semId=semget(key,nsems,semflg) key:为信号量集名称,可以指定为0455等数字,也可以为PC_PRIVATE nsems:创建几个 ...

  6. laravel中用到的ServiceProvide

    路由 全局限制 如果你希望路由参数可以总是遵循正则表达式,则可以使用 pattern 方法.你应该在 RouteServiceProvider 的 boot 方法里定义这些模式: 1 2 3 4 5 ...

  7. 利用数目找中位数(牛客第七场E)

    https://ac.nowcoder.com/acm/contest/887/E 树状数组做法(代码有注释) #include<bits/stdc++.h> using namespac ...

  8. By virtue of|sustain|post |scrape off |stretch|access to|take into account of|exploit|hasten|blur |idle|bored her to|account for|accused of|cruelty

    By virtue of this superior quality, this product is often sold out of stockin many areas. 我们的产品因其优秀的 ...

  9. 遍历一个文件夹,打印出该文件夹下所有的py文件

    import os def iterbrowse(path): for home, dirs, files in os.walk(path): for filename in files: yield ...

  10. C# SerialPort 读写三菱FX系列PLC

    1:串口初始化 com = , Parity.Even, , StopBits.One); 2:打开关闭串口 if (com.IsOpen) { com.Close();//关闭 } com.Open ...