Constexpr&const&mutable修饰符的使用

Constexpr&const&mutable修饰符的使用

Constexpr修饰普通函数

#include <iostream>  
using namespace std;  
  
constexpr int FuncInc(int num)  // 函数的参数与返回值类型必须都是数值型数据
{  
    return num++;  
}  
  
int main()  
{  
    // 在编译时,可以将FuncInc(5)替换成常量,编译器对返回值为常量的函数进行代码的优化  
    int Array[FuncInc(5)] = { 0 };  
    int i = 10;  
    FuncInc(i); // 变量在编译的时候,不会响应constexpr的优化请求,即不会对含有变量的函数进行优化  
}  

 

注:对于constexpr修饰的函数,如果其传入的参数可以在编译期算出来,那么这个函数就会产生编译期的值。如果传入的参数不能在编译时期计算出来,那么constexpr修饰的函数就和普通函数一样了。

如果不声明为constexpr会怎样?

#include <iostream>  
using namespace std;  
  
int FuncInc(int num)  
{  
    return num++;  
}  
  
int main()  
{  
    // 在编译时,可以将FuncInc(5)替换成常量,编译器对返回值为常量的函数进行代码的优化  
    int Array[FuncInc(5)] = { 0 }; // 如果不声明函数为constexpr,那么FuncInc就会被认为是变量,无法用于申请静态数组的空间  
    int i = 10;  
    FuncInc(i); // 变量在编译的时候,不会响应constexpr的优化请求,即不会对含有变量的函数进行优化  
}  

 

注:对函数体用constexpr关键字且函数体返回值是常数时,这个返回值可用于申请静态数组的空间,动态数组的空间,等等,作为常量使用,与常量的性质等同。

Constexpr修饰类的成员函数(与修饰普通函数类似)

#include <iostream>  
#include <string>  
using namespace std;  
  
class Cstudent  
{  
private:  
    string name;  
    float Geography;  
    float Math;  
    float Physics;  
    float Chinese;  
public:  
    Cstudent(string name, float Geography, float Math, float Physics, float Chinese)  
    {  
        this->name = name;  
        this->Geography = Geography;  
        this->Physics = Physics;  
        this->Chinese = Chinese;  
        this->Math = Math;  
    }  
    ~Cstudent() {};  
    constexpr float TotalAverage(float Mean) // 系统在编译阶段就对返回值为常量的成员函数做出优化,直接将相应位置的函数替换为常量  
    {  
        return 78.9;  
        // constexpr要求函数的返回值和形参必须都是数值型数据,不可以是字符串,字符等其他非数值型数据类型  
    }  
    void AverageValue()  
    {  
        cout << this->name << "的各科平均成绩为" << (Geography + Math + Physics + Chinese) / 4 << "分" << endl;  
    }  
};  
  
int main()  
{  
    Cstudent stud("超级霸霸强", 69, 85, 75, 63);  
    // 只有参数为常数,才可以保证函数输出值为常量,constexpr关键字才会起作用!  
    cout << "全体学生各科成绩的平均值为" << stud.TotalAverage(78.9) << endl;  
    stud.AverageValue();  
} 

 

 

Const修饰类的成员函数

把成员函数声明为const类型的意义

Const修饰类的成员函数时,表明被修饰函数无法改变成员函数的值,只能访问,不可更改!

#include <iostream>  
#include <string>  
using namespace std;  
  
class Cstudent  
{  
private:  
    string name;  
    float mark;  
public:  
    Cstudent(string name, float mark)  
    {  
        this->name = name;  
        this->mark = mark;  
    }  
    ~Cstudent() {};  
    void ShowValue() const // const表明函数不可改变任意成员函数的值,只允许访问,不允许修改  
    {  
        cout << this->name << "的分数是" << this->mark << "分" << endl;  
    }  
};  
  
int main()  
{  
    Cstudent stud("超级霸霸强", 99.99);  
    stud.ShowValue();  
}

​​​​​​​  

Mutable的使用

#include <iostream>  
#include <string>  
using namespace std;  
  
class Cstudent  
{  
private:  
    string name;  
    mutable float Geography;  
    float Math;  
    float Physics;  
    float Chinese;  
public:  
    Cstudent(string name, float Geography, float Math, float Physics, float Chinese)  
    {  
        this->name = name;  
        this->Geography = Geography;  
        this->Physics = Physics;  
        this->Chinese = Chinese;  
        this->Math = Math;  
    }  
    ~Cstudent() {};  
    // 当成员函数被声明为const类型后,在该成员函数中所有成员数据默认被声明为const类型,但是mutable例外  
    void AdditionAddMark(int Amark) const   // 注意const类型成员函数的形式
    {  
        //Math += Amark; // 修改非mutable变量被视为非法  
        Geography += Amark; // 修改成功  
        cout << "加入" << Amark << "分额外成绩" << endl;  
    }  
    void ShowInf()  
    {  
        cout << this->name << "的平均成绩为" << (Geography + Math + Physics + Chinese) / 4 << "分" << endl;  
    }  
};  
  
int main()  
{  
    Cstudent stud("超级霸霸强", 69, 85, 75, 63);  
    stud.AdditionAddMark(10);  
    stud.ShowInf();  
}  

 

一般mutable如何使用?

当我们声明成员函数为const类型时在函数体内部更改任何成员数据均为不合法,那我必须要修改某个成员变量的值,该怎么办呢?我们可以将该成员变量声明成mutable类型,这样我们就可以在const类型的成员函数中修改操作这个被声明为mutable类型的成员变量了。

注:mutable汉语意思是“可变的,易变的”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥肥胖胖是太阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值