C++ 之 const

【c++】——const详解 文章目录1、在.c中const 的用法2、在.cpp中const 的用法3、Const和static修饰的成员3.1const修改成员变量的处理3.2static修饰的成员变量4、int* const p和const int* p区别 首先,我们要来明确一下,什么是const,在我们通常的观念里面,我们会认为下面这个代码是错误的。 const int b = 20; b = 30; 因为const修饰的变量不能作为左值。初始化完成后值不能被修改。 1、在.c中const 的用法 在我之前的一篇博文中, 阅读详情
1. const修饰普通变量和指针
const修饰变量,一般有两种写法:
const TYPE value;
TYPE const value;
这两种写法在本质上是一样的。它的含义是:const修饰的类型为TYPE的变量value是不可变的。
对于一个非指针的类型TYPE,无论怎么写,都是一个含义,即value只不可变。

例如:
const int nValue;         //nValue是const
int const nValue;    // nValue是const

但是对于指针类型的TYPE,不同的写法会有不同情况,例如:
A. const char *pContent;
B. char * const pContent;
C. char const *pContent;
D. const char* const pContent;

对于前三种写法,我们可以换个方式,给其加上括号
A. const (char) *pContent;
B. (char*) const pContent;
C. (char) const *pContent;

这样就一目了然。根据对于const修饰非指针变量的规则,很明显,A=C.
- 对于A,C, const修饰的类型为char的变量*pContent为常量,因此,pContent的内容为常量不可变.
- 对于B, 其实还有一种写法: const (char*) pContent;
含义为:const修饰的类型为char*的变量pContent为常量,因此,pContent指针本身为常量不可变.
- 对于D, 其实是A和B的混合体,表示指针本身和指针内容两者皆为常量不可变

总结:
(1) 指针本身是常量不可变
(char*) const pContent;
const (char*) pContent;

(2) 指针所指向的内容是常量不可变
const (char) *pContent;
(char) const *pContent;

(3) 两者都不可变
const char* const pContent;

还有其中区别方法:
沿着*号划一条线,
如果const位于*的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量;
如果const位于*的右侧,const就是修饰指针本身,即指针本身是常量。

2. const修饰函数参数
const修饰函数参数是它最广泛的一种用途,它表示函数体中不能修改参数的值(包括参数本身的值或者参数其中包含的值)。它可以很好
void function(const int Var); //传递过来的参数在函数内不可以改变(无意义,因为Var本身就是形参)
void function(const char* Var); //参数指针所指内容为常量不可变
void function(char* const Var); //参数指针本身为常量不可变(也无意义, 因为char* Var也是形参)

参数为引用,为了增加效率同时防止修改。
修饰引用参数时:
void function(const Class& Var);//引用参数在函数内不可以改变
void function(const TYPE& Var); //引用参数在函数内为常量不可变

3. const 修饰函数返回值
const修饰函数返回值其实用的并不是很多,它的含义和const修饰普通变量以及指针的含义基本相同。
(1) const int fun1() 这个其实无意义,因为参数返回本身就是赋值。

(2) const int * fun2() 
调用时 const int *pValue = fun2();
我们可以把fun2()看作成一个变量,那么就是我们上面所说的1.(1)的写法,即指针内容不可变。

(3) int* const fun3() 
调用时 int * const pValue = fun2();
我们可以把fun2()看作成一个变量,那么就是我们上面所说的1.(2)的写法,即指针本身不可变。


4. const修饰类对象/对象指针/对象引用
const修饰类对象表示该对象为常量对象,其中的任何成员都不能被修改。对于对象指针和对象引用也是一样。
const修饰的对象,该对象的任何非const成员函数都不能被调用,因为任何非const成员函数会有修改成员变量的企图。
例如:
class AAA
{
   void func1();
   void func2() const;
}
const AAA aObj;
aObj.func1(); ×
aObj.func2(); 正确
const AAA* aObj = new AAA();
aObj->func1(); ×
aObj->func2(); 正确

5. const修饰成员变量
const修饰类的成员函数,表示成员常量,不能被修改,同时它只能在初始化列表中赋值。
class A
{
   …
   const int nValue;       //成员常量不能被修改
   …
   A(int x): nValue(x) {}; //只能在初始化列表中赋值
}

6. const修饰成员函数
const修饰类的成员函数,则该成员函数不能修改类中任何非const成员函数。一般写在函数的最后来修饰。
class A
{
   …
void function()const; //常成员函数, 它不改变对象的成员变量. 也不能调用类中任何非const成员函数。
}
对于const类对象/指针/引用,只能调用类的const成员函数,因此,const修饰成员函数的最重要作用就是限制对于const对象的使用。

7. const常量与define宏定义的区别
(1) 编译器处理方式不同
define宏是在预处理阶段展开。
const常量是编译运行阶段使用。

(2) 类型和安全检查不同
define宏没有类型,不做任何类型检查,仅仅是展开。
const常量有具体的类型,在编译阶段会执行类型检查。

(3) 存储方式不同
define宏仅仅是展开,有多少地方使用,就展开多少次,不会分配内存。
const常量会在内存中分配(可以是堆中也可以是栈中)。
[C++中const与指针 cpp] => C++中const关键字对指针的影响 最后,我们通过p3指向了一个非const的变量y,但由于p3是指向const对象的指针,所以不能通过它来修改所指向的对象。在上面的例子中,函数 foo 中的参数 p 是一个指向 const int 的指针,这表明它是只读的。在上面的例子中,p指向了一个非const的变量x,但它本身是const的,不能被修改。总之,在处理指针类型的变量时,const 关键字可以用于指针本身、指针所指向的对象,甚至可以用于函数的参数和返回值,以避免意外的修改和错误的使用。另外一种情况是指向const对象的指针。 阅读详情

相关推荐

3D医学影像分割新突破:手把手教你用MedSAM2快速标注CT/MRI数据(附3D Slicer插件配置)

本文详细介绍了如何利用MedSAM2模型进行3D医学影像分割,包括从环境准备到3D Slicer插件配置的全流程。通过实战案例和技巧分享,帮助医疗从业者快速掌握CT/MRI数据的高效标注方法,提升医学影像分析的准确性和效率。

ujm5678901的博客 307

C++ const的用法详解

const在C/C++中是十分重要的,如果单纯理解为“常量”那么你的格局就小了,今天在这里给大家介绍一下const在C++中具体详细的用法。

Mi-ronin的博客 6万+

PAM4信号测试实战:从眼图分析到误码率测量的技术演进

在高速数字系统设计中,信号完整性测试是确保数据传输可靠性的核心环节。其基本原理在于通过时域和频域分析,精确评估信号在传输过程中的质量衰减与失真。随着数据速率向56Gbps及以上演进,传统的NRZ编码面临带宽瓶颈,而PAM4调制技术通过在每个符号周期内承载更多比特信息,成为提升信道效率的关键路径。这项技术的工程价值在于,它允许在相同带宽下实现翻倍的数据吞吐量,这对于数据中心互联、PCIe 6.0及800G以太网等高速接口至关重要。然而,PAM4信号电平间距更小,对噪声和抖动极为敏感,导致其原始误码率显著升高,

weixin_42522817的博客 673

C++中const总结

1. const修饰普通变量和指针 const修饰变量,一般有两种写法: const TYPE value; TYPE const value; 这两种写法在本质上是一样的。它的含义是:const修饰的类型为TYPE的变量value是不可变的。 对于一个非指针的类型TYPE,无论怎么写,都是一个含义,即value只不可变。 例如: const int nValue; //nValue是const 

evilotus的专栏 800

STM32 ADC+DMA实战:手把手教你驱动XGZP6847A压力传感器(附完整代码)

本文详细介绍了如何使用STM32的ADC和DMA外设驱动XGZP6847A压力传感器,包括硬件连接、ADC配置、DMA高效传输实现以及数据处理与校准方法。通过实战案例和完整代码,帮助开发者快速掌握高精度压力数据采集技术,适用于工业控制和医疗设备等领域。

weixin_42570484的博客 414

从零打造M328晶体管测试仪:硬件DIY与烧录避坑指南

本文是一份详尽的M328晶体管测试仪DIY指南,涵盖从硬件选型、PCB设计、焊接组装到固件烧录的全过程。重点解析了使用AVR单片机与Arduino IDE环境时,如何正确设置熔丝位以避免“白屏”等常见问题,并提供了使用ISP编程器进行烧录的实操步骤与校准方法,帮助电子爱好者成功打造属于自己的高性价比元件测试工具。

weixin_29243063的博客 481

C语言 基础知识之const(const是什么,const的作用,const的使用)

一、const是什么 const是C/C++中的类型修饰符。 二、const的作用 1、对变量声明只读特性,保护变量值以防被修改 2、节省空间,避免不必要的内存分配。const修饰的变量在程序运行过程中只有一份拷贝 3、提高程序运行效率。编译器不为普通const常量分配存储空间,而是将他们保存在符号表中,这使得它成为一个编译期间的常量,没有了存储和读取内存的操作。 三、const的使用 1、const和普通变量 (1)编译器在编译过程中把...

李江林的博客 7万+

【代码审计】RuoYi定时任务漏洞:从SQL注入到RCE的攻防演变

本文深入剖析了RuoYi框架定时任务模块从早期版本无校验到引入黑白名单机制,再到被组合利用实现RCE的完整漏洞演变过程。文章详细分析了SQL注入与定时任务功能结合导致的远程代码执行漏洞,并总结了代码审计与安全开发的核心要点,为开发者提供了重要的安全参考。

yellow的专栏 709

Cpp关键字破解(一)【const】篇

const关键字总结 文章目录const关键字总结是什么初始化怎么用核心思想const引用指向const对象的指针&const指针多文件顶层const概念 是什么 以const(即单词constant)创建的变量的值不能改变,因为值不能修改,可以理解为常量,但是不存在真正的常量,只是在const的作用域中,该变量才不能被修改,超过了这个const的作用域,有可能是可以修改。 注意:const仅表示不能改变,不表示具体的变量类型,按照从右向左的方式来理解变量的命名,首先要说明这是一个什么类型的变量,其

日月既往,不可复追 832

机器人关节电机发热难题:从工程化角度拆解热管理解决方案(下)

摘要:本文系统分析了机器人关节电机与驱动系统的热管理方案,重点解决局部热点问题。针对不同功率场景提出了热管/均热板散热(适用于中小功率)和液冷散热(适用于高功率)的具体实施方案,详细比较了结构参数、材料选择、安装工艺和散热效果。特别强调了电机-驱动协同热管理的重要性,提出"工况联动+热路径共享"的核心理念,包括一体化散热结构设计和动态控制策略。最后介绍了热仿真与测试验证方法,确保方案的有效落地。通过系统级热管理,可显著降低温升(热点区域降温30-60℃),同时优化体积和能耗。

bother3000的博客 3345

北京大学MuMo多模态肿瘤分类模型复现与迁移学习

本项目复现了北京大学MuMo多模态肿瘤分类模型,并针对特定医学影像数据集进行迁移学习。MuMo模型通过整合CT、MRI、PET等多模态医学影像数据,显著提升了肿瘤分类准确性。项目内容包括环境配置(Python 3.8+、PyTorch 1.9+)、数据预处理(DICOM/NIfTI格式处理、强度归一化)、多模态数据集类实现,以及训练验证测试集划分。关键特点是采用MONAI医学影像处理库,实现三维医学影像的标准化预处理流程,并支持多种模态数据的并行加载与联合训练,为后续模型复现和迁移学习奠定基础。

max500600的博客 1245

转换成const_细谈c++的const

C++const 关键字小结const 是constant的缩写,本意是不变的,不易改变的意思。const 在C++中是用来修饰内置类型变量,自定义对象,成员函数,返回值,函数参数。1、C语言的const特点 const int a = 10;//不要把a看成常量 //a的本质 是变量 只是 只读变量c语言的const修饰全局变量 默认是(外部链接的)fun.c //c语言的co...

weixin_39801158的博客 177

基于Jeecgboot3.9.0的文档管理系统(四)

基于Jeecgboot3.9.0的文档管理系统(四)

宁波阿成的博客 259

C++——const详解

指针常量(顶层const): 本质是一个常量的指针,指针指向的地址不能改变,该地址存储的数据可以改变。常量指针(底层const): 指向常量的指针,,a是一个常量,p指向a的地址空间,a的值不能变,p的指向可以改变。更一般的,顶层const可以表示任意的对象是常量,对任何数据类型都适用,比如int、类、指针。 比较特殊的是:指针既可以是顶层const也可以是底层const,和其他类型有明显区别;const修饰变量主要有两个作用: 1、将变量定义为常量,保护被修饰的东西,防止意外的修改,增强程序的健壮性。co

kk阿彬 3万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

C++中 const的用法

const”在C++中用来修饰一个变量变为常量,本篇文章我们来介绍一下const的具体用法。

2301_79944784的博客 3581

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【CPP】const放在不同位置的作用

const & constexpr

记忆超差的本人的博客 1865

C++函数前后加const的区别

1、函数前后const 函数前const:普通函数或成员函数(非静态成员函数)前均可加const修饰,表示函数的返回值为const,不可修改。格式为: const returnType functionName(param list) 函数后加const:只有类的非静态成员函数后可以加const修饰,表示该类的this指针为const类型,不能改变类的成员变量的值,即成员变量为read only...

Gorgeous_mj的博客 2万+

c++ const 全面总结!

C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助。 Const 是C++中常用的类型修饰符,常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。一、Const作用 如下表所示:  二、Const的使用 1、定义常量 (1)const修饰变量,以下两

扎扎实实写代码的专栏 2万+

cpp const的作用

小Q在周末的时候和他的小伙伴来到大城市逛街,一条步行街上有很多高楼,共有n座高楼排成一行。 小Q从第一栋一直走到了最后一栋,小Q从来都没有见到这么多的楼,所以他想知道他在每栋楼的位置处能看到多少栋楼呢?(当前面的楼的高度大于等于后面的楼时,后面的楼将被挡住) 输入描述: 输入第一行将包含一个数字n,代表楼的栋数,接下来的一行将包含n个数字wi(1<=i<=n),代表每一栋楼的高度。 1...

qq_38677031的博客 1676

关于C++ const 的全面总结

C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助。Const 是C++中常用的类型修饰符,常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。一、Const作用 如下表所示:No.作用说明参

亿言 40万+

C、CPP的const

C和C++的const的区别本质上,C++的const为一个常量,所以可以初始化数组。而C的const为一个伪常量不可以初始化数组。 C: #include <stdio.h> void main() { const int num = 5;//C语言的const属于伪常量 int a[num];//所以此处报错 } CPP: #include <iostre...

上尉姚 238

C++ 顶层 const 和底层 const

本文详细解析了C++中const限定符的两种用法:顶层const和底层const。顶层const表示对象本身不可修改,底层const表示指针/引用指向的对象不可修改。

平平无奇的小甜菜 926

C++中const的使用

/ 推荐使用大写命名。

KodeScape 628

C/C++中的const

如果是 const 在 * 之前,如 const int *p ,表示指针 p 所指向的内容是常量,不能通过 p 来修改它指向的内容,但 p 本身可以指向其他地址;如果 const 在 * 之后,如 int * const p ,则表示指针 p 本身是常量,不能再指向其他地址,但可以通过 p 修改它指向的内容;- 在类中, const 成员函数是一个重要的概念。4).如果两个函数名相同,参数相同,但是一个是常函数一个是非常函数,那么这两个函数为函数重载,非常对象优先调用非常函数,常对象只能调用常函数。

2401_88249494的博客 2263

【C++】const 限定符

对C++当中的const类型部分的内容进行学习和整理,主要的参考资料是第五版C++ Primer的第2.4节。

YGGP的博客 1506

C++的const

在C++编程中,`const`关键字是保证程序安全性和稳定性的重要工具。它可以用来定义不可修改的变量、限定指针行为,甚至控制类成员函数的权限。通过合理使用`const`,开发者可以避免不必要的数据修改,提升代码的可读性和维护性。本文将深入探讨`const`在C++中的各种应用场景,帮助你充分理解并掌握这一关键特性。

weixin_44259499的博客 1285
上一篇: linux下编译C++程序出现错误 “*** stack smashing detected ***, eclipse解决方法
下一篇: 基于构建Spark开发环境
cosin_
博客等级 码龄17年 2粉丝 5原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值