知识回顾: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. UML-各阶段如何编写用例

    1.前文回顾 用例的根本价值:发现谁是关键参与者,他要实现什么目标? 需求分类,见<进化式需求>:制品,见<初始不是需求阶段>中的表4-1 2.各阶段编写何种用例,均针对下图展 ...

  2. 阿里OSS下载文件,提示The request signature we calculated does not match the signature you provided. Check your key and signing method

    提示说是签名不对,但没搞懂签名具体是啥,以为之前做过,有正确的,就一点点比对,最后发现竟然是下载的文件路径,里面必须是/,而不能是\或\\,搞得我哭笑不得.比如,要下载的文件路径是:soft/cszt ...

  3. TPO3-1Architecture

    Much of the world's great architecture has been constructed of stone because of its beauty, permanen ...

  4. Maven中settings.xml文件各标签含义

    原文地址:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension settings.x ...

  5. 4412开发板QtE系统下MT6620-wifi的测试

    基于iTOP4412系统烧写并启动之后,使用如下命令.wpa_passphrase XXX "YYY " >> /etc/wpa_supplicant.conf其中 X ...

  6. 2019-2020-1 20199324《Linux内核原理与分析》第二周作业

    一.知识点总结 1.冯诺依曼体系结构的要点: ①五大基本类型部件:运算器.控制器.存储器.输入设备.输出设备 ②用二进制来表示指令和数据 ③ 核心:存储程序计算机 2.常见的汇编指令 mov指令(l指 ...

  7. P2486 [SDOI2011]染色 区间合并+树链剖分(加深对线段树的理解)

    #include<bits/stdc++.h> using namespace std; ; struct node{ int l,r,cnt,lazy; node(,,,):l(l1), ...

  8. z-scores|zα

    6.2 Areas Under the Standard Normal Curve Property 4: Almost all the area under the standard normal ...

  9. git相关学习地址

    https://git-scm.com/book/zh/v2    这篇文章写得不错,值得一读

  10. 2019ICPC南京网络赛B super_log(a的b塔次方)

    https://nanti.jisuanke.com/t/41299 分析:题目给出a,b,mod求满足条件的最小a,由题目的式子得,每次只要能递归下去,b就会+1,所以就可以认为b其实是次数,什么的 ...