注意:这不是教程,是学习笔记,不适合初学者阅读!!!
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;
}