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

C++ 44 之 指针运算符的重载

 

#include <iostream>
#include <string>
using namespace std;class Students04{
public:int m_age;Students04(int age){this->m_age = age;}void showAge(){cout << "年龄是: " << this->m_age << endl;}~Students04(){cout << "析构" << endl;}
};// 利用智能指针管理new出来的students04对象的delete操作
class SmartPoint{
public:Students04* m_stu;SmartPoint(Students04* stu){this->m_stu = stu;}~SmartPoint(){if(this->m_stu){delete this->m_stu;this->m_stu = NULL;cout << "智能指针析构"<< endl;}}// 重载->Students04* operator->(){return this->m_stu;}// 重载*Students04& operator*(){return *(this->m_stu);}};int main()
{// 堆中申请空间// Students04* stu1 = new Students04(18); // 显示法// stu1->showAge();  //方法1:// (*stu1).showAge();  //方法2:解引用法// delete stu1;SmartPoint sp(new Students04(18));  // sp 是个实例化对象 对象不是指针,不能用->符号,所以要重载 sp->// sp 无法实现指针效果,再次重载->// sp 是个实例化对象 对象不是指针,不能用->符号,所以要重载 sp->// sp->->showAge();  // sp->  == this->m_stu// this->m_stu->showAge();// sp->->showAge();// 系统认为太丑了,所以两组箭头间化为1组sp->showAge();// sp无法实现指针效果,再次重载*(*sp).showAge();return 0;
}

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

相关文章:

  • onlyoffice在线预览加载优化
  • 依赖自动装配
  • mysql和redis的双写一致性问题
  • Qwen2——阿里巴巴最新的多语言模型挑战 Llama 3 等 SOTA
  • 等级考试3-2021年3月题
  • Web前端开发PPT:深入探索与实战应用
  • liunx常见指令
  • vscode设置成中文界面
  • python命名空间详解
  • 【日常记录】【vue】vite-plugin-inspect 插件的使用
  • mini web框架示例
  • 基于C#开发web网页管理系统模板流程-主界面统计功能完善
  • chromedriver114以后版本下载地址汇总chromedriver所有版本下载地址汇总国内源下载
  • x86计算机的启动初期流程 Linux 启动流程
  • P450Rdb: CYP450数据库--地表最强系列--文献精读24
  • ubuntu 22.04下载和安装
  • Fegin如何传参form-data文件
  • 解决 Visual C++ 17.5 __cplusplus 始终为 199711L 的问题
  • Mac M3 Pro安装Hadoop-3.3.6
  • mac下Xcode在iphone真机上测试运行iOS软件
  • Mysql(一):深入理解Mysql索引底层数据结构与算法
  • WebMvcConfigurer配置不当导致鉴权失败
  • 微信小程序毕业设计-实验室管理系统项目开发实战(附源码+论文)
  • C#:lock锁与订单号(或交易号)的生成
  • 计算机图形学入门11:图形管线与着色器
  • 正解 x86 Linux 内存管理
  • springboot读取配置时,读取到了系统环境变量
  • 平均召回(Average Recall,AR)概述
  • WWDC 2024 回顾:Apple Intelligence 的发布与解析
  • [Cloud Networking] SPDY 协议