当前位置: 首页 > news >正文

【C++初阶】类与对象(中)之取地址及const取地址操作符重载(了解即可)

在这里插入图片描述

👦个人主页:@Weraphael
✍🏻作者简介:目前学习C++和算法
✈️专栏:C++航路
🐋 希望大家多多支持,咱一起进步!😁
如果文章对你有帮助的话
欢迎 评论💬 点赞👍🏻 收藏 📂 加关注✨


目录

  • 一、const成员
  • 二、取地址及const取地址操作符重载
    • 2.1 特殊使用情况

一、const成员

const修饰的“成员函数”称之为const成员函数, const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

我们来看看下面的代码:

#include <iostream>
using namespace std;class Date
{
public:Date(int year, int month, int day){Year = year;Month = month;Day = day;}void Print(Date* const this){cout << Year << "-" << Month << '-' << Day << endl;}private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);d1.Print();const Date d2(2023, 5, 21);d2.Print();return 0;
}

在这里插入图片描述

  • 以上代码对于d2是编译不通过的,这是为什么呢?
  1. 当对象d1调用成员函数Print时,编译器会隐式自动地将该对象的地址作为第一个参数传递给成员函数,因此,在一个类的成员函数内部,可以使用this指针来访问该对象的成员变量和成员函数。this指针的类型:类名* const this,const修饰this,即成员函数中,不能修改this指针;
  2. 而对于对象d2,当调用成员函数时,编译器同样会自动将该对象作为第一个参数传递给成员函数,但不同的是,由于const修饰d2,因此this指针的类型应该是const Date* const this,因此不能给*this赋值,所以导致了报错。

因此,为了解决以上问题。C++是允许在函数后加上一个const,这样就能正常运行了:

#include <iostream>
using namespace std;class Date
{
public:Date(int year, int month, int day){Year = year;Month = month;Day = day;}void Print() const{cout << this->Year << "-" << this->Month << '-' << this->Day << endl;}private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);d1.Print();const Date d2(2023, 5, 21);d2.Print();return 0;
}

【程序结果】

在这里插入图片描述

【总结】

  1. 成员函数在后加上const后,普通和const修饰的对象都能用
  2. 但注意:由于const修饰的对象,其形参中的*thisconst修饰,也就说明不能对*this进行修改。因此,如果成员函数内要修改对象成员变量的函数千万不要用const修饰对象。
  3. 如果声明和定义分离,声明的函数和定义的函数都要加const

二、取地址及const取地址操作符重载

首先先来看看一下代码:

#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){Year = year;Month = month;Day = day;}
private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);const Date d2(2023, 5, 21);cout << &d1 << endl;cout << &d2 << endl;return 0;
}

【程序结果】

在这里插入图片描述

通过以上结果我们发现:const取地址操作符重载不用重新定义,编译器默认会生成。

当然,如果想自己定义也是可以的,代码如下:

#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){Year = year;Month = month;Day = day;}Date* operator&(){return this;}const Date* operator&() const{return this;}private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);const Date d2(2023, 5, 21);cout << &d1 << endl;cout << &d2 << endl;return 0;
}

【程序结果】

在这里插入图片描述

需要注意的是:重载const 取地址操作符时,返回的指针类型必须是const指针,否则会导致编译错误。

2.1 特殊使用情况

虽然这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如 不想让别人获取到指定的内容

#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){Year = year;Month = month;Day = day;}Date* operator&(){return nullptr;}const Date* operator&() const{return nullptr;}
private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);const Date d2(2023, 5, 21);cout << &d1 << endl;cout << &d2 << endl;return 0;
}

【程序结果】

在这里插入图片描述

http://www.lryc.cn/news/70203.html

相关文章:

  • 代驾公司如何管理司机
  • 面了一个5年经验的测试工程师,自动化都不会也敢喊了16k,我也是醉了····
  • ChatGPT:你真的了解网络安全吗?浅谈攻击防御进行时之网络安全新定义
  • LeetCode_DFS_困难_1377.T 秒后青蛙的位置
  • 第四十九天学习记录:C语言进阶:结构体
  • LeeCode [N字形变换]算法解析
  • CPU性能提升:流水线
  • C语言指针初级
  • C++的历史
  • 保姆级别!!!--全网绝对教你会!!教你如何使用MQTTFX连接阿里云平台中的设备----下期告诉你如何创建!
  • Unexpected token ‘‘‘, “‘{“type“:““... is not valid JSON
  • 关于C语言的杂记5
  • YOLOv5 vs YOLOv6 vs YOLOv7目标检测模型速度和准确度的性能比较——深入研究
  • 如何增加网站权重?有效提高网站权重的技巧方法
  • 路径规划 | 图解快速随机扩展树RRT算法(附ROS C++/Python/Matlab仿真)
  • 【Stable Diffusion WebUI】一篇文章教你如何安装和使用Stable Diffusion WebUI
  • Python篇——数据结构与算法(第二部分)
  • 人工智能之读懂CNN卷积神经网络
  • go手写Redis(1)之协议说明
  • Hadoop/HbBase/Hive/HDFS/MapReduce都是什么?
  • 羽毛球中级提高班课后总结
  • 多维时序预测 | Matlab基于最小二乘支持向量机LSSVM多维时间序列预测,LSSVM多变量时间序列预测
  • KDZK-F水轮发电机转子测试仪
  • I2C通信协议原理和MPU6050
  • 3.5 RDD持久化机制
  • Nginx(四)
  • 【fps系统重构】-观察cpu、memroy、io -iostat
  • iptables 添加,删除,查看,修改,及docker运行时修改端口
  • Liunx安装Android Studio
  • 8、Linux C/C++ 实现MySQL的图片插入以及图片的读取