【C++】const与shared_ptr使用中的一个细节

参考:记录以下boost::shared_ptr的一个使用细节

shared_ptr<T>::operator->返回的是T*类型指针,非const T*指针。因此通过const shared_ptr<T>&类型的ptr可以直接调用T各个原始的方法,不用担心const与非const问题
具体shared_ptr::operator->实现如下,摘自boost1.52.0版本boost\smart_ptr\shared_ptr.hpp

T * operator-> () const // never throws
{
      BOOST_ASSERT(px != 0);
      return px;
}

可以看出shared_ptr<T>operator->中的const修饰,是指shared_ptr<T>对象自身,而非shared_ptr所管理的对象

这个和普通原生指针还是有很大区别的,需要注意。

陈硕(@bnu_chenshuo)大牛对此有解释:const T*对应的是shared_ptr〈const T〉const shared_ptr〈T〉对应的是T* const。注意二者之间的差别

因此下面的代码是可以正确编译运行的:

#include <boost/shared_ptr.hpp>
#include <iostream>
 
using namespace std;
 
class Item
{
public:
    Item(){}
    ~Item(){}
     
    void set(int data) { data_ = data; }
     
    void print(void)
    {
        cout<<"Item::data_: "<<data_<<endl;
    }
     
private:
    int data_;
};
 
typedef boost::shared_ptr<Item> ItemPtr;
 
void foo(const ItemPtr& item)
{
    item->set(3); // 此处item对应于Item* const类型
}
 
int main ()
{
    ItemPtr item(new Item);  // 此处item对应于Item* 类型
    foo(item);
    item->print();
 
    return 0;
}```

以下是一个简单的shared_ptr的实现,仅用于参考。 ```c++ template<typename T> class shared_ptr { public: shared_ptr() : ptr(nullptr), ref_count(nullptr) {} shared_ptr(T* p) : ptr(p), ref_count(new int(1)) {} shared_ptr(const shared_ptr<T>& other) : ptr(other.ptr), ref_count(other.ref_count) { if (ref_count) ++(*ref_count); } ~shared_ptr() { dispose(); } shared_ptr<T>& operator=(const shared_ptr<T>& other) { if (this != &other) { dispose(); ptr = other.ptr; ref_count = other.ref_count; if (ref_count) ++(*ref_count); } return *this; } T* operator->() const { return ptr; } T& operator*() const { return *ptr; } bool operator==(const shared_ptr<T>& other) const { return ptr == other.ptr; } bool operator!=(const shared_ptr<T>& other) const { return !(*this == other); } bool operator<(const shared_ptr<T>& other) const { return ptr < other.ptr; } bool operator>(const shared_ptr<T>& other) const { return other < *this; } bool operator<=(const shared_ptr<T>& other) const { return !(other < *this); } bool operator>=(const shared_ptr<T>& other) const { return !(*this < other); } bool is_null() const { return ptr == nullptr; } int use_count() const { return ref_count ? *ref_count : 0; } T* get() const { return ptr; } private: T* ptr; int* ref_count; void dispose() { if (ref_count) { --(*ref_count); if (*ref_count == 0) { delete ptr; delete ref_count; } ptr = nullptr; ref_count = nullptr; } } }; ``` 该shared_ptr类实现了拷贝构造函数、拷贝赋值运算符、析构函数、箭头运算符、解引用运算符、相等运算符、不等运算符、比较运算符、is_null()方法、use_count()方法get()方法。 在构造函数中,初始化指针ptr为nullptr,引用计数ref_count为nullptr。在拷贝构造函数中,ptrref_count被复制,并且引用计数加1。在析构函数中,如果引用计数不为0,就减1,如果减到0,就删除指针ptr引用计数ref_count。在赋值运算符中,先dispose旧的shared_ptr,然后复制新的shared_ptr,最后增加引用计数。在箭头运算符解引用运算符中,返回指针ptr。在相等运算符不等运算符中,比较指针ptr。在比较运算符中,比较指针ptr。在is_null()方法中,判断指针ptr是否为nullptr。在use_count()方法中,返回引用计数ref_count的值,如果为nullptr,则返回0。在get()方法中,返回指针ptr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值