首页 > 其他分享 >34 The Mutable Keyword

34 The Mutable Keyword

时间:2024-09-03 17:38:39浏览次数:4  
标签:std const Keyword int 34 mutable Mutable

The Mutable Keyword

Referece: The Mutable Keyword in C++

mutable, it declares something that can change.

with const

In debug mode, if you want to know how many time a const declared method was called, dowever, becauseof const, you can not modify any of the class members, so you can not record the calling times. Now, you got the keyword mutable, it allowed that method to modify mutable declared class members.

#include <iostream>
#include <string>

class Entity
{
private:
	std::string m_Name;
	mutable int m_DebugCount = 0;
public:
	const std::string& GetName() const
	{
		m_DebugCount++;
		return m_Name;
	}
};

int main()
{
	const Entity e;
	e.GetName();
}

with lamdas

A lamda is basically like a little throwaway function that you write and assign to a variable quikly.

int x = 8;
auto f = [=]()
  {
    x++;
    std::cout << x << std::endl;
  };
f();

// error: 'x': a by copy capture cannot be modified in a non-mutable lambda.

you can fix it with a additional local variable y,

int x = 8;
auto f = [=]()
  {
    int y = x;
    y++;
    std::cout << y << std::endl;
  };
f();

but, it looks a little bit messy, especially there are lots of this kind of variables, so mutable, use it to declare a mutable lambda. (complier would translate as same as what mentioned above)

int x = 8;
auto f = [=]() mutable
  {
    x++;
    std::cout << x << std::endl;
  };
f();
std::cout << x << std::endl;
// output: 
// 9 (by f())
// 8

标签:std,const,Keyword,int,34,mutable,Mutable
From: https://www.cnblogs.com/zhihh/p/18395056/cpp-series-34-mutable

相关文章

  • 代码随想录算法训练营,9月2日 | 242.有效的字母异位词,349. 两个数组的交集,202. 快乐数,1
    哈希表理论基础1.根据关键码的值而直接进行访问的数据结构(直白来讲其实数组就是一张哈希表,哈希表中关键码就是数组的索引下标,然后通过下标直接访问数组中的元素);2.哈希表都是用来快速判断一个元素是否出现集合里;3.哈希函数:把值对应到哈希表的函数;哈希碰撞:映射到哈希表同一个索引......
  • 高密度、高速卡边缘连接器,ME1005610201091、ME1005610203071、ME1005613401311、ME100
    系列概述MiniCoolEdge是一款0.60mm高密度、高速卡边缘连接器,适用于新一代小型系统。这种精细间距解决方案支持多种板对板应用,如直角、夹层/共面,并提供电缆互连选件。这些连接器符合SFF-TA-1002、GenZ、EDSFF和OCPNIC3.0规范。常见应用包括固态驱动器、网络接口卡和......
  • JSP驾校管理平台9ar34(程序+源码+数据库+调试部署+开发环境)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表系统功能:学员,教练,驾校项目,报名信息,驾校车辆,练车预约,教学培训,成绩信息,缴费信息开题报告内容一、项目背景随着汽车保有量的不断增加,驾驶培训已成为社会......
  • P10934 西瓜种植 解题报告
    题目传送门这道题也可以用贪心来做,这里讲一下差分约束的做法。看到题中给出了\(m\)条限制性的语句就联想到差分约束(差分约束的题还是很显眼的)。做差分约束的题首先得把题面抽象成很多个不等式,所以我们先来转化一下题意。首先发现求最小值,那么先确定转化方向:将所有条件转换成......
  • Leetcode3234. 统计 1 显著的字符串的数量
    EverydayaLeetcode题目来源:3234.统计1显著的字符串的数量解法1:枚举左端点注意到,如果子串中的0非常多,多到0的个数的平方比1的个数都要大,那么这样的子串必然不是1显著子串。设cnt0为子串中的0的个数,cnt1为子串中的1的个数,那么必须满足:cnt0*cnt0<=......
  • 20240904_070346 mysql 存储过程 认识
    什么是存储过程存储过程的特点......
  • 20240904_080346 mysql 存储过程 创建与使用存储过程
    存储过程的使用修改结束符号将默认的句子结束符号由;改为$号的写法创建存储过程调用存储过程......
  • 20240904_090346 mysql 存储过程 查看表中的全部存储过程
    查看格式相关表格实操演示SELECT*FROMinformation_schema.routinesWHEREROUTINE_SCHEMA="my_school";......
  • 20240904_100346 mysql 存储过程 查看存储过程中的语句
    说明用法和查建表语句是一样的实操查到的完整内容为......
  • 343. 整数拆分(leetcode)
    https://leetcode.cn/problems/integer-break/dp,思路较为巧妙,需要考虑一个数至少能拆成两份这个点,且需要考虑到拆的这个数的值域是多少(1,i-1)且选择拆一次还是拆多次classSolution{public:intintegerBreak(intn){//f[i]表示拆分i成若干个整数的最大乘......