Access to class operator overload wrapped by std :: shared_ptr

The idea is that I want the class that was wrapped std::shared_ptrcan still be used just like they were not a pointer, for example, the = operator that was defined in my class can still be used after my class wrapped std::shared_ptr.

eg

template <class Ty> class shared_ptr_proxy : public std::shared_ptr<Ty> {
public:
    template<class Other> shared_ptr_proxy& operator=(const Other& rhs)
    {
        (*this->get()) = rhs;
        return *this;
    }
    template<class Other> explicit shared_ptr_proxy(Other * ptr) 
        : std::shared_ptr<Ty>(ptr){};
};

// usage :
shared_ptr_proxy<float> obj = shared_ptr_proxy<float>(new float);
obj = 3.14;

its work, but is there a way I don’t need to create shared_ptr_proxyor inherit a class from std::shared_ptr?

and

if I like it, is there some kind of warning I should take care of?

+1
source share
5 answers

, . - , , .

, , shared_ptr, , .

:

#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>

template <class Ty> class shared_ptr_proxy   {
    std::shared_ptr<Ty> ptr;
public:
    template<class Other> explicit shared_ptr_proxy(Other * p) 
        : ptr(std::shared_ptr<Ty>(p)){};

    template<class Other> shared_ptr_proxy& operator=(const Other& other)
    {
        *ptr = other;
        return *this;
    }

    operator Ty& () { return *ptr; }
    operator const Ty& () const { return *ptr; }
};

int main()
{
    std::vector<shared_ptr_proxy<int> > vec {
        shared_ptr_proxy<int>(new int(10)), 
        shared_ptr_proxy<int>(new int(11)), 
        shared_ptr_proxy<int>(new int(9))
    };
    vec.back() = 8;  //use assignment
    std::sort(vec.begin(), vec.end());  //sort based on integer (not pointer) comparison
    for (unsigned i = 0; i != vec.size(); ++i) {
        std::cout << vec[i] << ' ';  //output stored values
    }
}

#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>

template <class Ty> class shared_ptr_proxy : public std::shared_ptr<Ty>   {
public:
    template<class Other> explicit shared_ptr_proxy(Other * p) 
        : std::shared_ptr<Ty>(p){};

    template<class Other> shared_ptr_proxy& operator=(const Other& other)
    {
        *this->get()= other;
        return *this;
    }

    operator Ty& () { return *this->get(); }
    operator const Ty& () const { return *this->get(); }
};

int main()
{
    std::vector<shared_ptr_proxy<int> > vec {
        shared_ptr_proxy<int>(new int(10)), 
        shared_ptr_proxy<int>(new int(11)), 
        shared_ptr_proxy<int>(new int(9))
    };
    vec.back() = 8;  //the only thing that works
    std::sort(vec.begin(), vec.end());  //sort based on pointer values
    for (unsigned i = 0; i != vec.size(); ++i) {
        std::cout << vec[i] << ' ';  //outputs addresses
    }
}
+3

operator= , . , .

+1

, , , , , .

+1

, , , operator = shared_ptr, - shared_ptr. , , , .

++ 11, . decltype std::declval rvalue- std::forward . . .

, -: http://frigocoder.dyndns.org/svn/Frigo/Lang/ref

, :

  • operator = (ref&) operator = (ref&&) . - - operator = (const T&) . , , , .

  • , operator +=, - GCC. , : x += y x = (x + y), / . , .

  • Garbage collected using GC Boehm instead of reference counting. It's also a conscious choice of mine; link counting is a pretty bad alternative to garbage collection.

0
source

General dereference pointer:

std::shared_ptr<float> obj(new float);
*obj = 3.14;
-1
source

Source: https://habr.com/ru/post/1649420/


All Articles