【C++ | const成员】一文了解类的 const数据成员、const成员函数、const对象、mutable 数据成员

😁博客主页😁:🚀https://blog.csdn.net/wkd_007🚀
🤑博客内容🤑:🍭嵌入式开发、Linux、C语言、C++、数据结构、音视频🍭
⏰发布时间⏰:2024-06-14 23:57:00

本文未经允许,不得转发!!!



在这里插入图片描述

🎄一、概述

在C++中,const 关键字可以用来定义常量,并且const修饰的对象是必须初始化且不能被修改的。

在C++的类和对象中,使用const也有一些需要注意的:
1、对于类的const数据成员,要怎样初始化呢?
2、类的const函数是怎样的?有什么用处?怎样定义const函数?
3、使用const对象有哪些需要注意的?


在这里插入图片描述

🎄二、const 数据成员

const 数据成员:是指在类中声明的,使用 const 关键字修饰的数据成员。下面代码的Month就是 const 数据成员:

class CDate
{
...
private:
	int m_year;
	int m_mon;
	int m_day;
	const int Month;
};

const 修饰的对象必须要初始化,那么类的 const 数据成员怎样初始化呢?C++提供了两种方法:

  • 1、构造函数的成员初始化列表:
    成员初始化列表只有构造函数才有,在参数列表后使用分号(:)分隔开,下面以 CDate 为例,其构造函数如下;

    CDate(int year, int mon, int day):Month(12)
    {
    	m_year = year;
    	m_mon = mon;
    	m_day = day;
    }
    
  • 2、C++11的类内初始化:
    C++11标准支持类内初始化,就是在类中声明数据成员的时候就初始化。参考下面代码:

    class CDate
    {
    ...
    private:
    	int m_year;
    	int m_mon;
    	int m_day;
    	const int Month=12;	// C++11的类内初始化
    };
    

下面代码演示了上面两中方法:

// // g++ 16_member_init_list.cpp -std=c++11
#include <iostream>
using namespace std;

class CDate
{
public:
#if __cplusplus > 199711L // C++11
	CDate(int year, int mon, int day)
#else
	CDate(int year, int mon, int day):Month(12)
#endif
	{
		m_year = year;
		m_mon = mon;
		m_day = day;
	}

	void show()
	{
		cout << "Date: " << m_year << "." << m_mon << "." << m_day << ", Month=" << Month << endl;
	}

private:
	int m_year;
	int m_mon;
	int m_day;
#if __cplusplus > 199711L // C++11
	const int Month = 12; // C++11的类内初始化
#else
	const int Month;
#endif
};

int main()
{
	cout << __cplusplus << endl; // 打印 C++标准
	CDate date(2024,06,14);
	date.show();

	return 0;
}

在这里插入图片描述

🎄三、const 成员函数

const 成员函数:是指保证不会修改调用对象的成员函数。

const 成员函数在函数声明和定义时,会在函数参数的括号后面添加关键字 const,以 CDate 类为例:

void show() const; // 声明
void CDate::show() const // 声明
{
	// m_year = 1;	// 会报错:assignment of member ‘CDate::m_year’ in read-only object
	cout << "Date: " << m_year << "." << m_mon << "." << m_day << ", Month=" << Month << endl;
}

const 成员函数的几个注意点:

  • 1、const 成员函数本质上是修改了this指针的类型,改为 const 类类型 *this
  • 2、只要类方法不修改调用对象, 就应将其声明为 const。
  • 3、const 成员函数不能修改数据成员,除非该数据成员是 mutable 修饰的。
  • 4、const函数只能调用const函数,不能调用非const函数
  • 5、const 成员函数如果以引用的形式返回*this,那么它的返回类型将是const引用。
  • 6、基于 const 的函数重载,通过区分成员函数是否为 const 的,可以对该函数进行重载。因为const成员函数的this指针是const的,而非const成员函数的this指针没带const。
  • 67、构造函数不能声明、定义为const。

在这里插入图片描述

🎄四、mutable 数据成员

mutable 数据成员:是指在类中声明的,使用 mutable 关键字修饰的数据成员。永远不会是 const,即使它是 const 对象的成员。也就是说, mutable 数据成员在任何成员函数中都可以被更改。

下面例子演示了怎样声明、定义、使用 const 成员函数mutable 数据成员

// g++ 16_const_fun.cpp 
#include <iostream>
using namespace std;

class CDate
{
public:
	CDate(int year, int mon, int day):Month(12)
	{
		m_year = year;
		m_mon = mon;
		m_day = day;
		mu_Month = 0;
	}

	void show() const;

private:
	int m_year;
	int m_mon;
	int m_day;
	const int Month;
	mutable int mu_Month;
};

void CDate::show() const
{
	mu_Month = 1;	// mutable 数据成员在任何成员函数中都可以被修改
	cout << "Date: " << m_year << "." << m_mon << "." << m_day << ", mu_Month=" << mu_Month << endl;
}

int main()
{
	CDate date(2024,06,14);
	date.show();

	return 0;
}

在这里插入图片描述

🎄五、const 对象

const 对象:使用关键字 const 修饰并创建的对象是 const 对象。

关于const对象有几个注意点:
1、const对象 需要初始化,除非提供无参构造;
2、const对象 只能调用 const函数
3、非const对象 优先调用 非const函数 ,如果没有 非const函数 则选择 const函数

参照下面例子:

// g++ 16_const_object.cpp 
#include <iostream>
using namespace std;

class CDate
{
public:
	CDate(int year, int mon, int day)
	{
		m_year = year;
		m_mon = mon;
		m_day = day;
	}
	
	CDate(){}	// 1、const 对象必须初始化,除非提供无参构造

	void show()
	{
		cout << "Date: " << m_year << "." << m_mon << "." << m_day << ", not const" << endl;
	}
	
	void show() const
	{
		cout << "Date: " << m_year << "." << m_mon << "." << m_day << ", const" << endl;
	}

private:
	int m_year;
	int m_mon;
	int m_day;
};

int main()
{
	const CDate date;
	date.show();

	return 0;
}

在这里插入图片描述

🎄六、总结

👉本文介绍了类的 const数据成员、const成员函数、mutable 数据成员、const对象

在这里插入图片描述
如果文章有帮助的话,点赞👍、收藏⭐,支持一波,谢谢 😁😁😁

相关推荐

  1. const修饰成员函数

    2024-06-15 11:54:01       12 阅读
  2. 【C++ const成员函数使用】

    2024-06-15 11:54:01       38 阅读
  3. C++如何在const函数修改成员变量

    2024-06-15 11:54:01       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-15 11:54:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-15 11:54:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-15 11:54:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-15 11:54:01       18 阅读

热门阅读

  1. 深度解析服务发布策略之蓝绿发布

    2024-06-15 11:54:01       9 阅读
  2. 缓存缓存缓存

    2024-06-15 11:54:01       9 阅读
  3. Sklearn基础教程

    2024-06-15 11:54:01       8 阅读
  4. 网络安全突发事件应急预案

    2024-06-15 11:54:01       11 阅读
  5. 智能合约中权限管理不当

    2024-06-15 11:54:01       8 阅读
  6. Git教程II

    2024-06-15 11:54:01       10 阅读
  7. const与static区别

    2024-06-15 11:54:01       8 阅读
  8. 【C++】开源项目收集

    2024-06-15 11:54:01       8 阅读