, . - , , .
, , 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;
std::sort(vec.begin(), vec.end());
for (unsigned i = 0; i != vec.size(); ++i) {
std::cout << vec[i] << ' ';
}
}
#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;
std::sort(vec.begin(), vec.end());
for (unsigned i = 0; i != vec.size(); ++i) {
std::cout << vec[i] << ' ';
}
}