C++迈向精通,学习笔记:const方法

注意:这不是教程,是学习笔记,不适合初学者阅读!!!

const 方法

在方法的后面加上一个const关键字。

作用:说明在当前方法内部是绝对不会对于成员属性进行修改(不包括类属性)。const类型的方法只能被const方法调用。

const 添加的原则:能加就加,对于在逻辑上没有修改类属性值的方法,必须添加 const

设计一个程序来显示类方法get_x被调用的次数:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cassert>
#include <complex>
#include <numeric>
#include <algorithm>
#include <iomanip>
#include <climits>
#include <limits>
#include <chrono>

using namespace std;

class Point {
public :
    Point() : x(0), y(0) {}
    Point(int x, int y) : x(x), y(y) {}
    void set_x(int x) { this->x = x; }
    void set_y(int y) { this->y = y; }
    int get_x() const {
        // this->get_x_cnt++;
        // get_x_cnt++;
        Point::get_x_cnt++; // 每次调用get_x,get_x_cnt加1
        return this->x; 
    }
    int get_y() const { return this->y; }

    // 类方法:
    static int x_cnt() {
        return Point::get_x_cnt; // 返回类属性
    }

private :
    // 定义类属性
    // 现在来实现这样的一个需求:get_x被调用的次数
    // static的作用是使得该变量不需要依赖对象来访问
    static int get_x_cnt; // 声明类属性
    int x, y;
};

int Point::get_x_cnt = 0; // 初始化类属性,定义

int main() {
    const Point p(10, 15);
    cout << p.get_x() << "," << p.get_y() << endl;
    return 0;
}

如果想要统计当前对象,get_x被调用的次数,该如何设计程序呢?

将类属性变成成员属性:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cassert>
#include <complex>
#include <numeric>
#include <algorithm>
#include <iomanip>
#include <climits>
#include <limits>
#include <chrono>

using namespace std;

class Point {
public :
    Point() : get_x_cnt(0), x(0), y(0) {}
    Point(int x, int y) : x(x), y(y) {}
    void set_x(int x) { this->x = x; }
    void set_y(int y) { this->y = y; }
    int get_x() const {
        this->get_x_cnt++; // 每次调用get_x,get_x_cnt加1
        return this->x; 
    }
    int get_y() const { return this->y; }

    // 类方法:
    int x_cnt() const {
        return this->get_x_cnt; // 返回类属性
    }

private :
    int get_x_cnt;
    int x, y;
};

int main() {
    const Point p1(10, 15);
    cout << p1.get_x() << "," << p1.get_y() << endl;
    return 0;
}

结果发现报错了:原因是我们的const方法内不能出现对成员属性进行修改的操作。

这个时候怎么办呢?需要添加一个新的关键字: mutable

mutable 意为 “可变的”,表示该成员函数可进行类型的临时变化。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cassert>
#include <complex>
#include <numeric>
#include <algorithm>
#include <iomanip>
#include <climits>
#include <limits>
#include <chrono>

using namespace std;

class Point {
public :
    Point() : get_x_cnt(0), x(0), y(0) {}
    Point(int x, int y) : x(x), y(y) {}
    void set_x(int x) { this->x = x; }
    void set_y(int y) { this->y = y; }
    int get_x() const {
        this->get_x_cnt++; // 每次调用get_x,get_x_cnt加1
        return this->x; 
    }
    int get_y() const { return this->y; }

    // 类方法:
    int x_cnt() const {
        return this->get_x_cnt; // 返回类属性
    }

private :
    mutable int get_x_cnt;
    int x, y;
};

int main() {
    const Point p1(10, 15), p2(15, 20);
    cout << p1.get_x() << "," << p1.get_y() << endl;
    return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

若亦_Royi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值