C++笔记 inline类成员函数、const成员函数、mutable修饰符、this指针

C++笔记 inline类成员函数、const成员函数、mutable修饰符、this指针

inline类成员函数:

#include <string>
#include <iostream>
using namespace std;
class student {
public:
	explicit student(int age, int year);
	student();
	inline void text();

private:
	string name = "null";
	int year=0;
	int age=0;
};
inline void student::text(){
}
需要注意的是inline成员函数实现必须和声明在同一个头文件中,否则可能会报错。

const成员函数:

#include <string>
#include <iostream>
using namespace std;
class student {
public:
	explicit student(int age, int year);
	student();
	void text() const;

private:
	string name = "null";
	int year=0;
	int age=0;
};
void student::text() const {}
const成员函数表示不会修改类中的其他成员,也无法修改。

在这里插入图片描述

mutable修饰符


使用前缀mutable就可以在const成员函数内存进行修改。

this指针

普通成员函数:

类类型 *const this

const成员函数:

const 类类型 *const this

拷贝构造函数:

student::student(student& T): x(1) {
	cout << "拷贝构造函数" << endl;
}
	student a1;//默认构造函数
	student a2 = a1;
	student a3 (a1);
	student a4{ a1 };
	student a5 = { a1 };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值