指针运算符重载,智能指针
智能 指针 运算符 重载
2023-09-14 09:02:34 时间
#include<iostream>
using namespace std;
class person
{
public:
person() { cout << "无参构造的调用" << endl; }
void show()
{
cout << "年龄为:"<<18 << endl;
}
~person()
{
cout << "析构函数的调用" << endl;
}
};
void test()
{
person* p=new person;
p->show();
}
int main()
{
test();
system("pause");
return 0;
}
智能指针:托管new出来的对象的释放
#include<iostream>
using namespace std;
class person
{
public:
person() { cout << "无参构造的调用" << endl; }
void show()
{
cout << "年龄为:"<<18 << endl;
}
~person()
{
cout << "析构函数的调用" << endl;
}
};
//智能指针
class smartpoint
{
public:
smartpoint(person* person)
{
this->p = person;//指向在堆区开辟空间的地址
}
//重载->运算符号
person* operator->()
{
return this->p;
}
//重载*运算符号
person& operator*()
{
return *(this->p);
}
~smartpoint()
{
if (p != NULL)
{
delete p;
p = NULL;
}
}
private:
person* p;
};
void test()
{
/*person* p=new person;
p->show();*/
/*delete p;*/
//智能指针:托管new出来的对象的释放
smartpoint sp(new person());
//重载->运算符
//这里sp->返回的是person*
sp->show();//本质sp->->show(); 编译器简化为sp->show() sp.operator->()返回值是*person
(*sp).show();//*sp相当于-----sp.operator*(),返回值是(*person)
}
int main()
{
test();
system("pause");
return 0;
}
相关文章
- Effective C++第17条:要在单独的语句中使用智能指针来存储由new创建的对象
- 【C++】简单实现C++11的三种智能指针
- 智能合约审计指南
- 如何调试EVM智能合约(第1篇): 理解汇编
- ue4 弱指针_智能指针如何实现自动释放
- C++ 新特性学习(一) -- 概述+智能指针(smart_ptr)
- 浅析MetaForce原力元宇宙佛萨奇2.0智能合约系统开发逻辑详解丨佛萨奇2.0源码功能开发
- 达观数据:AIGC用于智能写作的技术综述
- C++ Boost 内存池与智能指针
- 高效智能办公,探索Office 2021专业增强版的新功能
- 365智能云打印怎么样?365小票无线订单打印机好用吗?
- 自动驾驶驶向未来,快来加入这场大赛,用实力驱动智能前行
- C++智能指针
- 澳门大学须成忠:「保守」无法推动智能驾驶行业进步 | 第四届全球智能驾驶峰会
- 智能指针
- C++11 unique_ptr智能指针详解
- C++优先级队列存储智能指针详解
- 浅谈C++ auto_ptr智能指针
- C++智能指针unique_ptr详解
- 云端Linux:开启智能新世界(云linux)
- 65英寸LG Signature OLED R卷屏智能电视开售 价格10万美元
- 利用 Oracle 触发器实现数据库智能管理(oracle触发器类型)
- Mimoto智能笔:让电脑显示器变身平板电脑
- 为什么商品视觉识别公司最后都去做了智能货柜?
- 技术SQL Server开启语音之旅:实现愿景中的智能语音助手(sqlserver的语音)
- Linux下的智能指针应用研究(linux智能指针)
- Oracle智能利用条件参数提升查询效率(oracle 写条件参数)
- 妙笔驰出Oracle 32位开启智能之旅(oracle 32bit)
- 森亿智能完成5亿元E轮融资
- 微软展示智能合成声音定制技术新玩法,让每一个角色都栩栩如生
- 浅析Boost智能指针:scoped_ptrshared_ptrweak_ptr
- C++智能指针实例详解