首页 > 编程语言 >C/C++ const关键字 解读

C/C++ const关键字 解读

时间:2023-09-03 16:22:56浏览次数:46  
标签:const reference int data modify C++ 关键字 pointer

The collocation between const and original pointer is confused to many people. There are two usages of it.
The first one is a variable pointer that points a constant data. i.e. const int* p

#include <iostream>

int main() {
	int a = 1, b = 2;
	const int *p = &a;

	p = &b;  // true
	*p = 3;  // false

	return 0;
}

The second one is a contant pointer that points a variable data. i.e. int* const p

#include <iostream>

int main() {
	int a = 1, b = 2;
	int* const p = &a;

	p = &b; // false
	*p = 3; // true

	return 0;
}

There is a good way to distinguish these two usages. You can judge them by the position of const and *.

  • If the const locates the left of the *, it means that the const keyword modifies the data *p, i.e. a constant data.
  • If the const locates the right of the *, it means that the const keyword modifies the data p, i.e. a constant pointer.

In addition, const can also collocates with C++ reference. But there is a litter difference between them.
That is because the difference between pointer and reference, which is that you can modify pointer pointing later, but you can't modify a reference pointing.
So there is no such situation that you modify the reference. You can just modify the data which is pointed by the reference.
Therefore, there is only one usage of reference, that is the const locates the left of the &. i.e. const int &p or int const &p.

标签:const,reference,int,data,modify,C++,关键字,pointer
From: https://www.cnblogs.com/hongyugao/p/17675112.html

相关文章

  • C++算法之旅、05 基础篇 | 第二章 数据结构
    常用代码模板2——数据结构-AcWing笔试用数组模拟而不是结构体使用结构体指针,newNode()非常慢,创建10万个节点就超时了,做笔试题不会用这种方式(优化是提前初始化好数组,但这样跟数组模拟没区别了,而且代码量很长)单链表(数组)使用两个数组,e存储val,ne存储next。空节点next用-1表......
  • 《C++并发编程实战》读书笔记(2):线程间共享数据
    1、使用互斥量在C++中,我们通过构造std::mutex的实例来创建互斥量,调用成员函数lock()对其加锁,调用unlock()解锁。但通常更推荐的做法是使用标准库提供的类模板std::lock_guard<>,它针对互斥量实现了RAII手法:在构造时给互斥量加锁,析构时解锁。两个类都在头文件<mutex>里声明。std::......
  • msvc++中的预编译头文件pch.hpp和stdafx.h
    预编译头文件在VisualStudio中创建新项目时,会在项目中添加一个名为pch.h的“预编译标头文件”。(在VisualStudio2017及更高版本中,该文件名为stdafx.h)此文件的目的是加快生成过程。应在此处包含任何稳定的标头文件,例如标准库标头(如)。预编译标头仅在它或它包含的任何......
  • C++引用
    首先引用是什么?在教程里说引用就是起别名。在我学过这一部分后觉得引用其实就是属于指针,有点像是指针的语法缩写。怎么说呢,我认为就两点三种传递引用是什么,引用的本质1、三种传递值传递不改变实参,地址传递和引用传递改变实参1#include<iostream>2usingnamespaces......
  • 使用synchronized关键字来同步多个线程操作同一个文件
    使用synchronized关键字来同步多个线程操作同一个文件importjava.io.FileWriter;importjava.io.IOException;publicclassFileSyncExample{privatestaticObjectfile=newObject();publicstaticvoidmain(String[]args)throwsInterruptedException{......
  • 如何使用C++11原子操作实现自旋锁
    什么是自旋锁?C++自旋锁是一种低层次的同步原语,用于保护共享资源的访问。自旋锁是一种轻量级的锁,适用于短时间的资源锁定。自旋锁的特点:当一个线程尝试获取已经被另一个线程占有的自旋锁时,这个线程会进入一个循环(自旋),在这个循环中它不断地检查锁是否已经被释放。如果锁已经被释放,那......
  • C++的基类和派生类构造函数
    基类的成员函数可以被继承,可以通过派生类的对象访问,但这仅仅指的是普通的成员函数,类的构造函数不能被继承。构造函数不能被继承是有道理的,因为即使继承了,它的名字和派生类的名字也不一样,不能成为派生类的构造函数,当然更不能成为普通的成员函数。在设计派生类时,对继承过来的成员变量......
  • c++ 堆排序
    堆排序主要分为两个函数:1、构建堆2、元素调整#include<iostream>usingnamespacestd;voidmaxHeap(inttree[],intn,inti){ if(i>=n) return; intlchild=i*2+1; intrchild=i*2+2; intmax=i; if(lchild<n&&tree[lchild]>t......
  • C++ Core Guidelines解析 电子书 pdf
    关注公众号:红宸笑。回复:电子书即可  在《C++CoreGuidelines解析》中,C++专家讲师RainerGrimm提炼出了CoreGuidelines中的精髓,去除了晦涩难懂的内容,分享了新的见解和背景,并提供了自己培训课程中经过充分测试的示例。对于使用C++11及后续版本C++的有经验程序员,G......
  • C++刷题输入输出和常用函数处理
    1.输入数字但非默认的十进制,比如输入的是十六进制数,但要转为十进制再进行别的处理。当我们在编程中处理十六进制数时,通常会将其表示为字符串。cin>>hex>>m;//输入十六进制,m会自动转十进制。2.int和string中单个字符互转strings="12345";inta0=s[0]-'0';//字符转......