【C++知识】const关键字

Linux系统调用 sigaction 的用法 sigaction 函数原型定义如下:   int sigaction(int signum, const struct sigaction *act,struct sigaction *oldact)   这个系统调用的作用是改变进程接收到的指定信号的行动。   使用这个函数需要包含头文件#include <signal>   函数参数说明如下:   signum : 说明具体信号,它可以是除了SIGKILL和SIGSTOP之外的任何有效信号值。   act : 将要安装的signum定义信号的新行动。   oldact: 用来保存signum定义信号的过去的行动。    立即下载
 

1.在变量声明中使用const

  • const int *const2 //const2是一个指针,指向了一个const int(常整形)变量
  • int const *const3 //与以上作用一致。 const3是一个指针,指向了一个const int(常整形)变量
  • int * const const4 // const4是一个const pointer(指针常量)指向了一个整形变量
  • int const *const const5 // const5是一个指针常量指向了一个整形常量

2.在函数参数中使用const

  • void Subroutine4(big_structure_type const &Parameter1);
    const表明了只可以对作为引用传递的Parameter1指向变量进行读取而不可修改

3.在类的成员函数中使用const

例如class1
class Class1{
 
void Method1();
 
int MemberVariable1;
}
 
void Class1::Method1(){
MemberVariable1=MemberVariable1+1; //Class1中的Method1是可以修改MemberVariable1
}
 
class Class2{
void Method1() const; //加了const关键字修饰以后,在Method1中不可修改成员变量
int MemberVariable1;
}
在函数声明中就加了const修饰。那么在多人协作的情况下,可能不同人实现类的不同成员方法。直接排除了借助修改成员变量实现函数行为的可能,减少了后续可能的冲突。    
注意:
值得注意的是,把一个成员函数声明为const可以保证这个成员函数不修改数据成员,但是,如果据成员是指针,则const成员函数并不能保证不修改指针指向的对象,编译器不会把这种修改检测为错误。
 
 
4. c++ 类中const成员变量的赋值方法

在头文件的类的定义中定义了一个const成员变量c++ 规则:

1、类定义中不能进行初始化,因为头文件中类的定义只是一个声明,并没有分配真正空间,因此变量是不存在的,因此是不能赋值的。

2、const 定义的变量是不能赋值

这可如何是好,声明中不能赋值,声明完还不能赋值。又不能不赋值。

解决方案:

1、在构造函数后的参数初始化列表中初始化

2、将const变量同时声明为 static 类型进行初始化。

 class A
    {
        const int nValue;         //成员常量不能被修改
        A(int x): nValue(x) { } ; //只能在初始化列表中赋值
     }

 

参考:
 
 
 
 
 
 
 
Codex入门24-效率倍增秘籍(终极总结:10个让你开发速度翻倍的Codex高级技巧) 专栏收官之作!经过前23篇的系统学习,你已经掌握了 Codex CLI 的核心用法。本文浓缩10个实战验证的高级技巧:管道化提示词将复杂任务拆成递进指令链,模板化 SKILL.md 打造个人技能库,Shell 别名让操作快如闪电,多模型切换省钱又高效……这不是理论课,每个技巧都配有可直接运行的代码。掌握全部10招,构建你的 AI 开发工作流闭环,让开发速度真正翻倍! 阅读详情

相关推荐

Vivado新手必看:5分钟搞定FPGA程序固化到Flash(附MCS文件生成全流程)

本文为FPGA新手详细解析了如何将程序固化到Flash存储器的完整流程。核心在于理解SRAM工艺FPGA断电易失的特性,并利用Vivado工具将比特流文件转换为Flash可识别的MCS文件。文章重点拆解了生成MCS文件的关键配置步骤、硬件连接要点,以及通过JTAG烧写Flash的实战操作,并提供了常见启动失败问题的排查清单,帮助开发者实现设计从调试到产品部署的关键跨越。

weixin_29230901的博客 352

使用信号处理函数sigaction报错

在使用信号处理函数signaction()时,会用到struct sigaction,在头文件中也包含了'signal.h',但是仍然会报错,会提醒不允许使用不完整的类类型。解决办法:在代码最上面加上一个宏定义:#define _XOPEN_SOURCE 700。

weixin_45595932的博客 2243

【生物识别模组介绍】静脉识别模组

静脉识别技术是依据流动的静脉血液中的血红蛋白对近红外光有所吸收,通过近红外光照射手指、手掌、面部、腕部呈现的分布图像,再使用特定的算法对图像进行特征提取、分析比对,从而进行身份认证识别。静脉结构相同几率为34亿分之一,每个人的静脉结构具有独特性,而且静脉特征图像自成年后终身不变,不受外部环境、疾病等影响,是人体最稳定的生物特征之一。商务合作联系:newmagicmission@gmail.com图1.0。

upupDay's blog 900

关于 struct sigaction 不允许使用不完整类型

在文件的开始处,(引用头文件的上边) 添加这个宏。

villou的博客 1045

Costas环参数调优指南:QPSK载波同步中的环路滤波器设计技巧

本文深入解析QPSK载波同步中Costas环的参数调优,特别是环路滤波器的设计技巧。通过分析星座图异常现象,提供从理论计算到MATLAB仿真的实战调优指南,涵盖环路带宽、阻尼系数等关键参数在不同信噪比场景下的选择策略,旨在解决载波同步失锁、性能下降等工程难题。

weixin_29035147的博客 546

Autosar结合simulink的汽车应用层软件开发实例

本文基于汽车应用开发案例,并结合Autosar相关知识,生成具有Autosar标准的汽车应用层开发代码,以供学习和参考。以上就是本篇内容,主要是利用Simulink创建Autosar接口并进行端口映射的方法。

m0_64899535的博客 2152

C++ 变量的声明和定义

为了允许把程序拆分成多个逻辑部分来编写,C++语言支持单独编译(separate compilation)机制,该机制允许将程序分割为若干个文件,每个文件可被独立分离式编译。 为了支持分离式编译,C++语言将声明和定义区分开来。 变量的声明有两种情况:一种是需要建立存储空间的。例如:inta在声明的时候就已经建立了存储空间。另一种是不需要建立存储空间的。 ...

风轻淡淡的博客 6015

RT_Thread Studio使用——USB虚拟串口(VCP)

RT_Thread Studio使用——USB虚拟串口(VCP)

weixin_44735205的博客 3374

C++类的const类型成员变量

#include&lt;iostream&gt; using namespace std; class MyClass { public: MyClass(const int a, float b); ~MyClass(); const int const_varible; private: }; //只能在构造函数参数行中对const类型成员变量初始化 //不能在构造函数中初始化...

A person does not really understand something until after teaching it to a computer. Donald.E.Knuth 1581

多目标跟踪中使用trackeval评估dancetrack运行结果

trackeval使用

qq_46144849的博客 1783

YOLOv11与Roboflow数据预处理实战:从原始数据到高性能模型

摘要 Roboflow平台与YOLOv11的结合为计算机视觉开发者提供了高效的工作流。Roboflow提供数据集管理、预处理、增强和导出的一站式解决方案,支持多种标注格式并与YOLOv11无缝集成。YOLOv11在YOLO系列基础上进行了架构创新,包括深度宽度优化、C3k2机制和C2PSA注意力机制,提供从纳米版到超大版的多种模型变体。 使用Roboflow的流程包括:注册工作区、收集/导入数据(支持本地和公开数据集)、专业标注(含SAM辅助)、预处理(尺寸调整、归一化等)和数据增强(几何变换和颜色调整)。

本博客聚焦 YOLOv11 全流程落地,涵盖架构优化、数据集处理、训练技巧与多场景部署,兼及国产数据库、Java 开发与 AI 模型应用,内容兼顾理论与工程实践,为开发者提供系统干货,助力高效提升技术能力。 350

C++的关键字const知识点总结

浏览了很多大佬的关于介绍C++中的关键字const的使用说明以后发现以下面的博客从文字叙述方面介绍的比较详细。而且是浏览量很高的博文,有很多详细的知识点介绍。 https://blog.youkuaiyun.com/Eric_Jo/article/details/4138548 https://blog.youkuaiyun.com/flowing_wind/article/details/81209845 这篇博...

qq_42261630的博客 223

ABAP 里解构引用操作符 ->* 的使用方法详解

解除引用操作符->*可用于将“类型为REF TO …的引用变量”的值(即该引用所指向内存)取出,从而访问其实际值或内部组件。具体地,若dref是一个“引用变量”,则表达式dref->*代表对该引用进行“解除引用”,使得程序按静态类型将其视为实际类型的值对象。例如,若lr类型声明为,则lr->*表示“将 lr 引用的INT4对象看作INT4类型并访问其值”。注:若 “数据引用变量” 的静态类型是通用类型DATA,则只能在这种上下文中使用->*。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 480

C++知识--const关键字

c++知识--const关键字

仰望星空 434

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

C/C++ const关键字知识笔记

C/C++ const关键字知识笔记 文章目录C/C++ const关键字知识笔记const修饰变量const与引用const与指针const修饰函数 const作为C/C++的一个关键字,用于修饰一个变量,表明该变量是一个常量,不可修改。 const修饰变量 const与引用 int a = 0; const int b = a; //正确,const变量b赋值为a,且存在值拷贝过程 const int &c = a; //错误,使用&,c即为a,a改变c即改变,单c为const修饰,

Blog of Chauncy Wang 251

深度学习工具包 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")

C++知识点之const关键字

C++知识点之const关键字1 const的用法1.1 修饰变量用于定义常量变量,这样这个变量在后面就不可以再被修改:const int iValue = 10; iValue = 20; //编译报错,iValue不可被修改1.2 修饰函数入参 如果输入参数采用“指针传递”,那么加const 修饰可以防止意外地改动该指针。 例如:void StringCopy(char *strDestina

loude1992的博客 362

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

C++基础知识-关键字const

2.2 没有 const reference,因为引用只是对象的别名,引用不是对象,不能用 const 修饰。1.2. 自身是常量的指针(常量指针,const pointer)2.1 指向常量的引用(reference to const)1.1. 指向常量的指针(pointer to const)注:被 const 修饰(在 const 后面)的值不可改变。

m0_72562997的博客 380

C/C++知识点总结:const关键字

C/C++总结文档:const 关键字。该文档总结了C++中const关键字的特点以及其使用规则。

ThreeWheelCat的博客 708

C/C++ Const关键字知识点整理

C/C++ Const关键字知识点整理 参考:博客 const是constant的缩写,名词意思有 常数;常量,形容词意思有 不变的;一贯的。 在C++中,const用来修饰内置类型变量(int; double; char等);自定义对象(结构体或自定义类等); 成员函数;返回值;函数参数。 const指定一个语义约束,编译器会强制实施这个约束,允许告诉编译器某值是保持不变的。如果确实有某个值保持...

ASCE_S的博客 515

c++中const关键字补充知识点

const 修饰类对象的时候,表示常量对象,此时,不能调用类中的非const函数!非const函数指原型声明中结尾不加const的函数!const 修饰iterator迭代器,表示迭代器是常量迭代器,不能对迭代器进行加减(++, --)操作(* const)const_iterator 表示迭代器所指向的对象是常量(内置类型或class),此时可以对迭代器进行增减,但是不能修改所指向的值或对象的值

沉墨笙箫的专栏 582

C++ const关键字的使用知识点总结

const的使用const声明一个常量在C++中,有变量和常量之分,常量在程序运行中不允许被改变,主要有两种声明方式。 const关键字 const double PI=10;//声明了一个PI常量,并定义为10 #define 关键字 #define PI 3.1415 //声明了一个PI常量,并定义为10由于const声明的常量是有类型的,而#define没有,所以const更

mikedadong的专栏 991

【一天一个C++小知识】002.C++中const和volatile关键字

欢迎扫描二维码关注微信公众号 深度学习与数学   [每天获取免费的大数据、AI等相关的学习资源、经典和最新的深度学习相关的论文研读,算法和其他互联网技能的学习,概率论、线性代数等高等数学知识的回顾] ...

kevin_zhao_zl的博客 314

C/C++知识点梳理之static关键字与const关键字

1.static关键字修饰变量: 局部变量:局部变量指在代码块{}中定义的变量,只在代码块内有效,缺省时为存储方式为自动变量或者动态变量,即程序执行到变量定义处才为变量分配存储空间,当改代码段执行结束是释放该存储空间。 static修饰局部变量时,则改变变量存储方式(生命周期),是变量成为静态局部变量,即在编译过程中为其分配存储空间,直到程序退出才释放这块空间,其生命周期为整个程序。这样使得该变...

陶超的博客 356

C++重要易混淆知识点:const关键字

表示指向区域的数据,是不变的,但是可以变换指向,如一个指针指向内容为int=10的区域,10是不可变的,但是指针可以变换指向为int=20的内存区域。其中,10和20都是不可以更改的//可以理解为只读指针*p = 20;//错误!,*p所指向区域的值是无法修改的,10是固定的p = &num2;//正确,可以修改指向为num2的地址。

qq_51365682的博客 272

C++基础知识复习 const关键字

没事翻了下C++ primer,开卷有益吧,加深下印象,留个复习记录如下:1、const 指针与指向 const对象的指针const int * p;/*指向const int 的指针*/int * const p;/*指向int的const指针*/记忆方法:从右往左读,*读作is a pointer to。2、const形参在非引用或非指针传值时,const修饰符无意义,因为复制传值的时候,不可能改变实参的值。const int &与int &不同可作为区分函数重载的标识。没有int const &,引用

enjolras的专栏 653

【c++基础知识——const关键字修饰的几种类型】

const 关键字可以修饰变量、引用和指针。它的作用是使被修饰的对象在其生命周期内不可修改。

XWWW668899的博客 515
上一篇: vscode无法使用C/C++插件格式化cpp代码
下一篇: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ a
风尘仆
博客等级 码龄15年 28粉丝 138原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值