首页 > 其他分享 >33 CONST

33 CONST

时间:2024-09-02 17:36:48浏览次数:3  
标签:CONST val 33 modify will int new const

CONST

Reference: CONST in c++

If you decalre something with const, you're saying that you are not going to modify that thing.

Combning with pointers

const int* a = new int;
// or 
int const* a = new int; 
// they mean that you can not modify the content of the memory address to which a pointed.

int* const a  = new int;
// it means that you can not modify what memory address a pointed to.

const int* const a = new int;
// both above can not

Read it backward, like the compiler does. For instance :

  • const int * A; ==> "A is a pointer to an int that is constant."
    (or, depending on how you prefer to write it)

  • int const* A; ==> "A is a pointer to a const int"

  • int * const A; ==> "A is a const pointer to an int."

  • const int* const A; ==> "A is a const pointer to an int that is constant".

Const int * A does not mean that A actually points to a const variable. It just means that the compiler will not allow you to modify the pointed value through A.

for instance :

int val = 10;
int const * a = &val;
*a = 30; //this will NOT compile, you're not allowed to modify "val" through "a".
val = 30; //this will compile. 

Same with :

int val = 10;
int val2 = 30;
const int * const A = &val; 

A = &val2; //will NOT compile : you can't modify what A points to.
*A = 30; //will NOT compile : you can't modify val through A

val = 30; //this will compile, val is not constant

Compiling things:

// the code
 const int MAX_AGE = 90;
 int* a = new int;
 a = (int*)&MAX_AGE;
 *a = 85;
 std::cout << *a << " " << MAX_AGE << std::endl;

// The output:  85 90

//I used the memory inspector in debug mode and saw the value pointed by &MAX_AGE actually being changed from 90 to 85 . So what the hell? Why is 90 the output for _MAX_AGE_, if the memory was actually modified!??

//I took one step further and inspected the generated .asm file (as taught by Cherno) to realize that the compiler optimized the code by replacing all read operations of MAX_AGE with the literal 90 . That cleared everything!! It's reasonable for a compiler to eliminate the need of accessing a variable if its never gonna change: just replace its occurrence with the said value from the get-go. One less operation each time it's used.

With classes and methods

class Entity
{
private:
  int m_X, m_Y;
  mutable int var; // even in a const decalred method, you can modify this variable
public:
  int GetX() const // const after the method name declares that it would not modify the class member.
  {
    var = 5; // you can modify it;
    return m_X;
  }
  
  int GetX()
  {
    m_X = 5;
    return m_X;
  }
};

void PrintEntity(const Entity& e)
{
  // const declare means that you can not modify the completely things of e, so if you call one method without const declared, you'll get an error
  std::cout << e.GetX() << std::endl;
}


int main()
{
  Entity e;
  
}

标签:CONST,val,33,modify,will,int,new,const
From: https://www.cnblogs.com/zhihh/p/18393154/cpp-series-33-const

相关文章

  • 电子秤方案的主控芯片选型SIC8833
     电子秤的方案设计功能比较简单,四个压力模块和ADC芯片以及再加个主控芯片大约就构成了其核心功能的器件要求。ADC芯片的功能是将压力模块所得到压力值转化为可显示的数值,在通过LED或者LCD屏展现出来,就是后面我们测量物品或体重所得到的重量斤数。 主控芯片就是MCU,它是方案的......
  • 33. 指针和函数
    1函数形参改变实参的值#include<stdio.h>voidswap1(intx,inty){ inttmp; tmp=x; x=y; y=tmp; printf("x=%d,y=%d\n",x,y);}voidswap2(int*x,int*y){ inttmp; tmp=*x; *x=*y; *y=tmp;}intmain(){ inta=3; intb=5......
  • 20240905_000339 mysql 存储过程 用户自定义变量
    自定义变量的特点一个@符号定义自定变量打印自定变量另一种定义方式查询赋值......
  • 20240905_010339 mysql 存储过程 局部变量
    ......
  • 深入理解C++中的const:函数参数与成员函数的最佳实践
    const关键字在C++中有多种用途,它的主要作用是定义不可修改的变量或数据,使得代码更加安全和清晰。具体使用场景包括:定义常量变量:使用const定义的变量在初始化后不能被修改。这样可以防止代码中对该变量的意外修改。constintmax_value=100;max_value=200;//错误:尝试......
  • 20240908_030339 编程剪辑 读取音频对像获得音频时长
    需求有一个目录里存放了多个音频文件我们要获取这些音频文件的相关信息编写一个方法接收音频文件的目录返回音频信息列表示例......
  • 详细认识指针(一) --指针的概念、指针的变量和地址、const修饰指针、指针的运算、野指
    前言:一提到指针,大家的第一反应是什么?很难理解,No,No,No。其实指针这个知识还是很贴近生活的,接下来我把我的感悟分享给大家。指针的概念指针的定义 首先,我们知道计算机上CPU(中央处理器)在处理数据的时候,需要的数据是从内存中读取的,处理后的数据也会放回到内存中。这个......
  • ISO 23374-1-2023 中文(part 2)
    ISO23374-1-2023Intelligenttransportsystems-Automatedvaletparkingsystems(AVPS)-Part1:Systemframework,requirementsforautomateddrivingandforcommunicationsinterface.智能交通系统——自动代客泊车系统(AVPS)——第一部分:系统框架,自动驾驶和通......
  • 1339:【例3-4】求后序遍历
    第一步:    找根节点(先序遍历:根,左子树,右子树)第二步:     找根节点的左子树(先序遍历:左子树,根,右子树)第三步:     找根节点的右子树模版代码:(满分代码)#include<bits/stdc++.h>usingnamespacestd;strings1;//先序遍历strings2;//中序遍历//l......
  • P3320 [SDOI2015] 寻宝游戏 与 P10930 异象石 与 CF176E Archaeology
    思路:考虑按照dfn序将关键点的集合排序后为\(a_0,a_1,\cdots,a_k\),则答案为:\[\frac{\sum\limits_{i=0}^k\operatorname{dis}(a_i,a_{(i+1)\bmodk})}{2}\]简单证明一下:需要找出包含一些关键点的最小联通导出子图。则随便以一个关键点为根,对于子树内没有关键点的子树直接......