2024年最全C++进阶之路---何为智能指针?,我的腾讯C C++面试经历分享

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

cin >> a >> b;
if (b == 0)
	throw invalid_argument("除0错误");
return a / b;

}
void Func()
{
SmartPtr sp1(new int);
SmartPtr sp2(new int);
cout << div() << endl;
}
int main()
{
try
{
Func();
}
catch (const exception& e)
{
cout << e.what() << endl;
}
return 0;
}


### 2.智能指针的原理


       上述的SmartPtr还不能将其称为智能指针,因为它还不具有指针的行为。指针可以解引用,也可以通过->去访问所指空间中的内容,因此:AutoPtr模板类中还得需要将\* 、->重载下,才可让其像指针一样去使用。



template
class SmartPtr
{
public:
SmartPtr(T* ptr = nullptr)
: _ptr(ptr)
{}
~SmartPtr()
{
if (_ptr)
delete _ptr;
}
T& operator*() { return _ptr; }
T
operator->() { return _ptr; }
private:
T* _ptr;
};


总结一下智能指针的原理:



> 
>        **1.具有RAll特性  
>         2.重载operator\*和operator->,具有和指针一样的行为。**
> 
> 
> 




---


## 三、智能指针的分类


### 1.std::auto\_ptr


       C++98版本的库中就提供了auto\_ptr的智能指针。


       下面演示的auto\_ptr的使用及问题。其原理是:管理权转移。



namespace GoodQ
{
template
class auto_ptr
{
public:
auto_ptr(T* ptr)
:_ptr(ptr)
{}
auto_ptr(auto_ptr& sp)
:_ptr(sp._ptr)
{
// 管理权转移
sp._ptr = nullptr;
}
auto_ptr& operator=(auto_ptr& ap)
{
// 检测是否为自己给自己赋值
if (this != &ap)
{
// 释放当前对象中资源
if (_ptr)
delete _ptr;
// 转移ap中资源到当前对象中
_ptr = ap._ptr;
ap._ptr = NULL;
}
return this;
}
~auto_ptr()
{
if (_ptr)
{
cout << “delete:” << _ptr << endl;
delete _ptr;
}
}
// 像指针一样使用
T& operator
()
{
return _ptr;
}
T
operator->()
{
return _ptr;
}
private:
T* _ptr;
};
}
int main()
{
std::auto_ptr sp1(new int);
std::auto_ptr sp2(sp1); // 管理权转移

 // sp1悬空
 *sp2 = 10;
 cout << *sp2 << endl;
 cout << *sp1 << endl;
 return 0;

}


       结论:auto\_ptr是一个失败设计,很多公司明确要求不能使用auto\_ptr


### 2.std::unique\_ptr


       C++11中开始提供更靠谱的unique\_ptr.其原理是:防止拷贝。



namespace GoodQ
{
template
class unique_ptr
{
public:
unique_ptr(T* ptr)
:_ptr(ptr)
{}
~unique_ptr()
{
if (_ptr)
{
cout << “delete:” << _ptr << endl;
delete _ptr;
}
}
// 像指针一样使用
T& operator*()
{
return _ptr;
}
T
operator->()
{
return _ptr;
}
unique_ptr(const unique_ptr& sp) = delete;
unique_ptr& operator=(const unique_ptr& sp) = delete;
private:
T* _ptr;
};
}
int main()
{
std::unique_ptr sp1(new int);
return 0;
}


### 3.std::shard\_ptr


       C++11中开始提供更靠谱的并且支持拷贝的shared\_ptr。


       shared\_ptr的原理:是通过引用计数的方式来实现多个shared\_ptr对象之间共享资源



> 
>   **1.shared\_ptr在其内部,给每个资源都维护了着一份计数,用来记录该份资源被几个对象共享。  
>         2.在对象被销毁时(也就是析构函数调用),就说明自己不使用该资源了,对象的引用计数减1。  
>         3.如果引用计数是0,就说明自己是最后一个使用该资源的对象,必须释放该资源;  
>         4.如果不是0,就说明除了自己还有其他对象在使用该份资源,不能释放该资源,否则其他对象就成野指针了。**
> 
> 
> 



template
class shared_ptr
{
public:
shared_ptr(T* ptr = nullptr)
:_ptr(ptr)
, _pcount(new int(1))
{}
template
shared_ptr(T* ptr, D del)
: _ptr(ptr)
, _pcount(new int(1))
, _del(del)
{}
void release()
{
if (–(*_pcount) == 0)
{
_del(_ptr);

		delete _pcount;
	}
}
~shared_ptr()
{
	release();
}
shared_ptr(const shared_ptr<T>& sp)
	:_ptr(sp._ptr)
	, _pcount(sp._pcount)
{
	++(*_pcount);
}
// sp1 = sp3
shared_ptr<T>& operator=(const shared_ptr<T>& sp)
{
	if (_ptr != sp._ptr)
	{
		release();

		_ptr = sp._ptr;
		_pcount = sp._pcount;

		++(*_pcount);
	}
	return *this;
}
// 像指针一样
T& operator*()
{
	return *_ptr;
}
T* operator->()
{
	return _ptr;
}
int use_count() const
{
	return *_pcount;
}
T* get() const
{
	return _ptr;
}

private:
T* _ptr;
int* _pcount;
function<void(T*)> _del = [](T* ptr) {delete ptr; };
};


std::shared\_ptr的循环引用问题



struct ListNode
{
int _data;
shared_ptr _prev;
shared_ptr _next;
~ListNode() { cout << “~ListNode()” << endl; }
};
int main()
{
shared_ptr node1(new ListNode);
shared_ptr node2(new ListNode);
cout << node1.use_count() << endl;
cout << node2.use_count() << endl;
node1->_next = node2;
node2->_prev = node1;
cout << node1.use_count() << endl;
cout << node2.use_count() << endl;
return 0;
}


循环引用分析:




![img](https://img-blog.csdnimg.cn/img_convert/d3f100682138910f27bd337200d97d42.png)
![img](https://img-blog.csdnimg.cn/img_convert/c9ec263d591b3c152949665543804ca3.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618668825)**

l;
	return 0;
}

循环引用分析:

[外链图片转存中…(img-mQKyioUA-1715752243392)]
[外链图片转存中…(img-9JVquZFo-1715752243393)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值