C++类static静态成员变量和const常量成员变量(和引用相同)初始化方法

类中常量数据成员的初始化 一. 测试代码头文件,在类中定义两个常量数据成员 //============================================================== // ExamConstDataMemberInitInClass.h // // begin : 2012-07-01 // author : zwq // describe: 类中常量 阅读详情

a为const常量成员

c为static静态成员

    常量和引用,必须通过参数列表进行初始化。
    静态成员变量的初始化也颇有点特别,是在类外初始化且不能再带有static关键


#include<stdio.h>
#include<iostream>
using namespace std;
class T{
	public:
		T(int b):a(b)
		{
			;
		}
		const int a;
		static int c;
};
int T::c = 14;
int main()
{
	T t(12);
	cout << t.a << endl;
	cout << t.c << endl;
	return 0;
}


关于C++ 类数据成员初始化的一点总结【为什么类定义中不能初始化成员变量】 往往被初学者忽视的是,C++类中的数据初始化是有jia 阅读详情

相关推荐

Anomalib实战:5分钟搞定图像异常检测模型训练(附完整代码)

本文提供了一份基于Anomalib框架的图像异常检测快速实战指南。文章详细介绍了从环境搭建、数据准备到模型训练与推理的完整流程,重点推荐了训练速度快、效果稳定的PatchCore模型,并附带了可运行的完整代码,旨在帮助开发者快速上手工业质检等实际应用。

weixin_29039773的博客 419

类中静态变量与const常量成员的初始化

类中静态变量与const常量成员的初始化

仗剑天涯 2270

Keil5与ARM编译器V5安装指南:从下载到配置全流程解析

本文提供Keil5与ARM Compiler V5的完整安装与配置指南。针对新版Keil5默认不包含V5编译器导致老工程编译失败的问题,详细解析了从软件下载、授权管理到获取并正确安装ARM Compiler V5(AC5)的全流程,确保开发者能顺利搭建兼容老项目的稳定开发环境。

weixin_29158047的博客 874

C++普通变量、C++静态成员变量、C++成员常量、C++静态成员常量的初始化方法

    最近复习C++的一些知识,遇到了一个我比较容易混淆、傻傻分不清的知识点,那就是C++的变量的类型初始化或赋初值,大致有普通成员变量、静态成员变量、成员常量这三种,还有一种组合的静态成员常量。    看到这几种数据成员时很尴尬,经常就把某两种搞混了,所以为了方便和好理解,在前辈的基础上做个小总结。    为了直观简洁的呈现这种对比,采用代码对比的方式完成这个事情。包括四种数据成员,进行四种初...

buknow的博客 6197

Dify工作流构建案例:迭代驱动的多维深度搜索

为了克服这些挑战,我们可以设计一个能够智能协同内部知识库与外部互联网搜索,并最终由大型语言模型(LLM)进行深度整合与精准回答的高级工作流。

同名公众号:dify实验室 2291

Gamit10.71添加新的天线类型

添加新的天线类型需要涉及到四个文件:antmod.dat,rcvant.dat,guess_rcvant.dat,hi.dat。 注意:格式要一致!!! 举例说明,我要添加一个JCA229的天线(带JCAD天线罩),对应接收机使用的是南方的South Net-S9。

Penny的博客 2710

C++初始化类的常量数据成员、静态数据成员、常量静态数据成员

有关const成员、static成员、const static成员的初始化:1、const成员:只能在构造函数后的初始化列表中初始化(C++98);   (C++11提供了类内初始化,因此内类常量可在声明类时进行类内初始化,因此该类内常量的作用域属于类级,即该类的所有对象均具有同一个值。)#include &lt;iostream&gt; using namespace std; class A ...

wk_bjut_edu_cn的博客 8581

华为OD机考2025B卷 - 报文响应时间(Java & Python& JS & C++ & C )

华为OD机考2025B卷 - 报文响应时间

算法大师! 8082

条款05:了解C++默默编写并调用哪些函数

什么时候一个空类,不再是空类了呢?当c++编译器处理之后。 如果没有定义,编译器会为你产生四个函数: 1)  默认构造函数 2)  Copy构造函数 3)  赋值构造函数 4)  析构函数 注意这些函数都是public且是inline的。 这里还有一点要说明的:不是说编译器一定会为你产生这些函数,而是说,当这些函数被调用时,它们才会被编译器创建出来。   下面主要谈一谈赋值构造函...

weixin_33769207的博客 139

24届数字IC验证——SV+UVM基础知识汇总(一)

汇总整理在面试过程中常见的问题,如果都能回答下来,那么面试肯定不成问题的分别构建不同抽象级的transaction,来实现复杂的激励。(以前是单一层次的sequence,对激励复用、上层复用不塌友好)不同层级之间的sequence没有继承关系,相互之间进行转换低层次的sequence从高层次的sequencer中获取高层次的sequence,并将其转换为低层次的sequence,再通过低层次的sequencer发送出去如何连接:低层次的sequencer包含高层次sequencer的句柄。

qq_42135020的博客 3145

论文必备 - YOLOv11热力图可视化,支持指定模型,指定显示层,设置置信度,以及10种可视化实现方式

本文带来的是YOLOv11热力图可视化功能,支持,以及。我们经常看到一些论文里绘制了不同的热力图,一方面能够直观的感受其模型的有效性,另一方面也丰富了论文内容。特别是在使用了注意力模块的网络中,热力图就可以验证注意力机制是否真正聚焦到了预期的重要特征上,以便对模型的有效性和合理性进行评估。例如这篇文章中展示的,就很能够表达作者改进后的模型相比之前模型的优越性。

Limiiiing的博客 5503

【C++】类中静态(static)成员变量、静态(static)成员函数和静态常量(static const)的应用

本文介绍了C++中静态成员变量、静态常量和静态成员函数的特性、应用、常见的软件设计模式以及单例模式、工厂模式的实现。静态成员变量、静态常量和静态成员函数可以帮助我们更好地组织数据和操作,并提高代码的效率和可读性。

crr411422的博客 1万+

Python中的字典(Dictionary)学习

上一章节,我们学习了列表(List) 和 元组(tuple) 来表示有序集合。而我们在讲列表(list)的时候,我们用了列表(list) 来存储用户的姓名。那么如果我们为了方便联系这些童鞋,要把电话号码也添加进去,该怎么做呢?但是这样很不方便,我们把电话号码记录下来,就是为了有什么事能及时联系上这些童鞋。如果用列表来存储这些,列表越长,我们查找起来耗时就越长。

热情、奔放、快乐编程! 502

类的成员变量初始化总结

1. C++变量的初始化方式 首先把需要初始化的成员变量分为几类: Ø  一般变量(int) Ø  静态成员变量(static int) Ø  常量(const int ) Ø  静态常量(static const int)  对应的初始化方式是: Ÿ  一般变量可以在初始化列表里或者构造函数里初始化,不能直接初始化或者类外初始化 Ÿ  静态成员变量必须在类外初始化 Ÿ  常

Tham 在思索中前行! 1万+

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

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

static和const 初始化问题总结

const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间。 static表示的是静态的。类的静态成员函数、静态成员变量是和类相关的,而不是和类的具体对象相关的。即使没有具体对象,也能调用类的静态成员函数和成员变量。一般类的静态函数几乎就是一个全局函数,只不过它的作用域限于包含它的文件中。 在C++中,static静态成员变量不能在类的内部...

Hello,小高同学 2458

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

17. C++ static、const 和 static const 类型成员变量声明以及初始化

static 关键字只能用于类定义体内部的声明中,定义时不能标示为 static。

谢谢大家的关注 1432

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

C++之const和static的区别

const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间。 static表示的是静态的。类的静态成员函数、静态成员变量是和类相关的,而不是和类的具体对象相关的。即使没有具体对象,也能调用类的静态成员函数和成员变量。一般类的静态函数几乎就是一个全局函数,只不过它的作用域限于包含它的文件中。 在C++中,static静...

zyc2018的博客 4933

C++中static const与static constexpr的类内变量成员初始化

1.一般static类内成员变量 class A { public: static int i1; static bool b1; static char c1; static float f1; static double d1; //以下错误,不能将一般static类型的变量在类内初始化,只能进行声明 //static int i2 = 2; //static bool b2 = true; //static char c2 = 'c'; //static float f2 =

mxyhktk的博客 4232

C++ static、const和static const类型成员变量声明以及初始化

原文链接:https://blog.youkuaiyun.com/tobefxz/article/details/14109697 下面的文章是我从网上找了好久,整理了好久的,对我有很大帮助,原址已经记不住了,算我自己原创的了哇! C++ static、const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会...

Keep Fighting! 912

C++类中const, static, reference等类型成员的初始化

有些成员变量的数据类型比较特别,它们的初始化方式也和普通数据类型的成员变量有所不同。这些特殊的类型的成员变量包括: a. 常量型成员变量(const) b. 引用型成员变量 (&amp;) c. 静态成员变量 (static) d. 整型静态常量成员变量 static const int e. 非整型静态常量成员变量 static const double 1. 常量型成员初始化:只能...

随意的风的专栏 3452

C++ const 与 static 详解:变量、函数、类成员的全面解析

本文系统解析了C++中const与static关键字的核心用法。const用于定义不可修改的常量,包括基本类型、指针、引用及类成员变量,强调初始化时机和不可变性。static则控制变量生命周期和可见性,包含静态局部变量(函数内持久化)、静态成员变量(类级别共享)和静态成员函数(不依赖实例的类工具方法)。文章还展示了二者在单例模式等设计模式中的综合应用,并比较了C++11引入的constexpr等现代特性。正确使用这些关键字能提升代码安全性、可维护性和性能优化。

保持分享欲 1626

C++ static、const和static const、引用以及它们的初始化

const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间。       static表示的是静态的。类的静态成员函数、静态成员变量是和类相关的,而不是和类的具体对象相关的。即使没有具体对象,也能调用类的静态成员函数和成员变量。一般类的静态函数几乎就是一个全局函数,只不过它的作用域限于包含它的文件中。       在C++中,sta

huthnli的专栏 2242

VC++类静态成员变量和const常量的初始化方法

C++类静态成员变量和const常量在定义类的时候就必须初始化,否则都会编译出错。

王学斌的博客 2001

C++ static、const和static const 以及它们的初始化

<br />      const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间。<br />      static表示的是静态的。类的静态成员函数、静态成员变量是和类相关的,而不是和类的具体对象相关的。即使没有具体对象,也能调用类的静态成员函数和成员变量。一般类的静态函数几乎就是一个全局函数,只不过它的作用域限于包含它的文件中。<br />      在C++中,static静态成员变量不能在类的内部初始化。在类的内部只是声明,定义必须在类定义体的

5万+

【C++】十、类和对象之const、static修饰成员变量/方法

一、构造函数的初始化列表 二、const修饰成员方法、变量 三、static修饰成员方法,成员变量 (一)static修饰成员变量 (二)static修饰成员方法 四、判断生成了几个对象

qq_43411555的博客 673
上一篇: linux下ioctl函数学习
下一篇: C++ const函数返回值必须为const引用
SprintfWater
博客等级 码龄15年 242粉丝 139原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值