介绍

mutable的中文意思是易变的,是C++的一个关键字。它的作用就是允许修改被const修饰的对象的成员变量。

常用场景

什么情况下我们会使用到mutable?

一般我们会用const修饰get类的函数,明确函数内部不会修改任何成员变量,但是如果内部的成员变量需要多线程读写,我们必须加锁保证多线程安全。毫无疑问,加锁操作是要修改锁对象的,此时就用到了mutable

#include <mutex>
class A
{
public:
    int get() const
    {
        std::lock_guard<std::mutex> lk( m );
        return value;
    }

    void set( int v )
    {
        value = v;
        return;
    }
private:
    mutable std::mutex m; // 不加mutable,编译就会报错
    int value;
};
int main()
{
    A a;
    return;
}

还有比如我们使用std::set:保存对象,当我们修改对象中非key的值时也需要用到mutable,不然就得重新构造对象,设置非key的值,再重新塞入set中。

#include <set>
class A
{
public:
    void set( int v ) const
    {
        value = v;
        return;
    }
    bool operator<( const A& rhs ) const
    {
        return key < rhs.key;
    }
private:
    int key;
    mutable int value; // 不加mutable,编译就会报错
};

int main()
{
    std::set<A> s;
    s.insert( A() );
    s.begin()->set( 10 );
    return 0;
}

std::set的迭代器类型总是const_iterator。这很好理解,因为对象的在std::set中的位置是根据其key值确定的,如果允许修改,那么std::set结构就不对了,再加上编译器无法确定函数内是否会修改key,所以通过其迭代器对象操作的函数必须是const修饰的。同理,std::mapfirst也是如此。

结合以上两种情况,我们总结一下就能得出mutable可以用来修饰const对象中那些不会影响外部观察的成员。

C++系列总结——mutable关键字的更多相关文章

  1. volatile关键字和mutable关键字

    如果不用volatile关键字会如何?可能会造成一个后果就是:编译器发现你多次使用同一个变量的值,然后它可能会假设这个变量是不变的值,并且把这个变量的值放入寄存器中,方便下一次使用,提高存取速度. 一 ...

  2. const成员函数和mutable关键字

    一.const成员函数 class MyClass { public: void fun() const { } private: int m_nValue; } const成员函数内不能修改成员变量 ...

  3. 可变lambda, lambda使用mutable关键字

    关于lambda的捕获和调用 C++ primer上对可变lambda举的例子如下: size_t v1=42; auto f=[v1] () mutable{return ++v1; }; v1=0 ...

  4. C++98常用特性介绍——mutable关键字

    讲mutable前,先讲一下const函数,讲const函数前,先讲一下函数前后加const的区别 一.C++函数前后加const的区别 1)函数前加const:普通函数或非静态成员函数前均可加con ...

  5. 面试问题之C++语言:mutable关键字

    转载于:https://www.cnblogs.com/xkfz007/articles/2419540.html mutable关键字 mutable的中文意思是"可变的,易变的" ...

  6. 【面试普通人VS高手系列】volatile关键字有什么用?它的实现原理是什么?

    一个工作了6年的Java程序员,在阿里二面,被问到"volatile"关键字. 然后,就没有然后了- 同样,另外一个去美团面试的工作4年的小伙伴,也被"volatile关 ...

  7. C++中的mutable关键字

    mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词. 在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量,将永远 ...

  8. mutable关键字

    mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词.在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量(muta ...

  9. 深入理解C++中的mutable关键字

      mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词. 在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量,将 ...

随机推荐

  1. 32 ArcToolBox学习系列之数据管理工具箱——属性域(Domains)的两种创建及使用方式

    属性域分为两类,一种是范围域,一种是编码的值,下面将两个一起介绍,其中涉及到的编码,名称,只是试验,并非真实情况. 一.首先新建一个文件型地理数据库,将数据导入或者是新建要素类都可以 二.打开ArcT ...

  2. 去掉input在type="number"时右边的上下箭头

    加了代码之后: input::-webkit-outer-spin-button, input::-webkit-inner-spin-button{ -webkit-appearance: none ...

  3. css基础样式

    1.行间样式:在标签中添加<style>属性      格式:标签名 style="样式:样式值1;样式2=样式值2"   2.内嵌样式:在<head>&l ...

  4. NP-Completeness理解

    今天大年初一,哪里也没去,在家里重新看了下IOA的NP问题.感觉看明白了. 首先定义下: 所谓P问题是指所有能在多项式复杂度解决的问题,比如排序算法,n*n复杂度解决问题. 有些问题目前没有多项式复杂 ...

  5. [Swift]LeetCode453. 最小移动次数使数组元素相等 | Minimum Moves to Equal Array Elements

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  6. [Swift]LeetCode466. 统计重复个数 | Count The Repetitions

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  7. [Swift]LeetCode628. 三个数的最大乘积 | Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  8. [Swift]LeetCode653. 两数之和 IV - 输入 BST | Two Sum IV - Input is a BST

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  9. [Swift]LeetCode700. 二叉搜索树中的搜索 | Search in a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  10. 【机器学习】--线性回归中L1正则和L2正则

    一.前述 L1正则,L2正则的出现原因是为了推广模型的泛化能力.相当于一个惩罚系数. 二.原理 L1正则:Lasso Regression L2正则:Ridge Regression 总结: 经验值 ...