C++智能指针

: _ptr(ptr)

{}

~SmartPtr()

{

if(_ptr)

delete _ptr;

}

private:

T* _ptr;

};

void MergeSort(int* a, int n)

{

int* tmp = (int*)malloc(sizeof(int)*n);

SmartPtr sp(tmp);

}

int main()

{

try {

int a[5] = { 4, 5, 2, 3, 1 };

MergeSort(a, 5);

}

catch(const exception& e)

{

cout<<e.what()<<endl;

}

return 0;

}

智能指针的原理

上面的smartptr还不能将其称为指针,还需要重载->,*才能像指针一样。

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;

};

struct Date

{

int _year;

int _month;

int _day;

};

int main()

{

SmartPtr sp1(new int);

*sp1 = 10

cout<<*sp1<<endl;

SmartPtr sparray(new Date);

sparray->_year = 2018;

sparray->_month = 1;

sparray->_day = 1;

}

std::auto_ptr

C++98版本就提供了auto_ptr的智能指针。

#include

class Date

{

public:

Date() { cout << “Date()” << endl;}

~Date(){ cout << “~Date()” << endl;}

int _year;

int _month;

int _day;

};

int main()

{

auto_ptr ap(new Date);

auto_ptr copy(ap);

// auto_ptr的问题:当对象拷贝或者赋值后,前面的对象就悬空了

// C++98中设计的auto_ptr问题是非常明显的,所以实际中很多公司明确规定了不能使用auto_ptr

ap->_year = 2018;

return 0;

}

template//底层实现

class AutoPtr

{

public:

AutoPtr(T*ptr = NULL)

:_ptr(ptr)

{}

~AutoPtr()

{

if(_ptr)

delete _ptr;

}

AutoPtr(AutoPtr& ap)
_ptr(ap._ptr)

{

ap._ptr = NULL;

}//拷贝后,ap与其管理的资源断开联系,避免一块空间被多个对象使用

AutoPtr& operator=(AutoPtr& ap)

{

// 检测是否为自己给自己赋值

if(this != &ap)

{

// 释放当前对象中资源

if(_ptr)

delete _ptr;

// 转移ap中资源到当前对象中

_ptr = ap._ptr;

ap._ptr = NULL;

}

return *this;

}

T& operator*() {return *_ptr;}

T* operator->() { return _ptr;}

private:

T* _ptr;

};

std::unique_ptr

C++11中提供更靠谱的unique_ptr

int main()

{

unique_ptr up(new Date);

// unique_ptr的设计思路非常的粗暴-防拷贝,也就是不让拷贝和赋值。

unique_ptr copy(ap);

return 0;

}

template//底层实现

class UniquePtr

{

public:

UniquePtr(T * ptr = nullptr)
_ptr(ptr)

{}

~UniquePtr()

{

if(_ptr)

delete _ptr;

}

T& operator*() {return *_ptr;}

T* operator->() {return _ptr;}

private:

// C++98防拷贝的方式:只声明不实现+声明成私有

UniquePtr(UniquePtr const &);

UniquePtr & operator=(UniquePtr const &);

// C++11防拷贝的方式:delete

UniquePtr(UniquePtr const &) = delete;

UniquePtr & operator=(UniquePtr const &) = delete;

private:

T * _ptr;

};

std::shared_ptr

C++11中开始提供更靠谱的并且支持拷贝的shared_ptr

int main()

{

// shared_ptr通过引用计数支持智能指针对象的拷贝

shared_ptr sp(new Date);

shared_ptr copy(sp);

cout << “ref count:” << sp.use_count() << endl;

cout << “ref count:” << copy.use_count() << endl;

return 0;

}

template //底层实现

class SharedPtr

{

public:

SharedPtr(T* ptr = nullptr)
_ptr(ptr)

, _pRefCount(new int(1))

, _pMutex(new mutex)

{}

~SharedPtr() {Release();}

SharedPtr(const SharedPtr& sp)
_ptr(sp._ptr)

, _pRefCount(sp._pRefCount)

, _pMutex(sp._pMutex)

{

AddRefCount();

}

// sp1 = sp2

SharedPtr& operator=(const SharedPtr& sp)

{

//if (this != &sp)

if (_ptr != sp._ptr)

本次面试答案,以及收集到的大厂必问面试题分享:

字节跳动超高难度三面java程序员面经,大厂的面试都这么变态吗?

r(ptr)

, _pRefCount(new int(1))

, _pMutex(new mutex)

{}

~SharedPtr() {Release();}

SharedPtr(const SharedPtr& sp)
_ptr(sp._ptr)

, _pRefCount(sp._pRefCount)

, _pMutex(sp._pMutex)

{

AddRefCount();

}

// sp1 = sp2

SharedPtr& operator=(const SharedPtr& sp)

{

//if (this != &sp)

if (_ptr != sp._ptr)

本次面试答案,以及收集到的大厂必问面试题分享:

[外链图片转存中…(img-i793kkXR-1718765673360)]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值