Вот такая странная проблема. Ниже я привел код класса auto_ptr
из STL. Вопрос: почему, если убрать оператор
auto_ptr& operator=(auto_ptr_ref<_Tp> __ref)
(я пометил его комментариями "// !!!!!!!!!!" ),
перестает компилироваться код вроде следующего:
auto_ptr<myclass> m;
m = new myclass; // здесь!
с руганью о ненахождении подходящего оператора присваивания??
Кто знает, объясните мне, глупому, в чем дело...
Cut here:
------------------------------------
namespace std
{
template<class _Tp1> struct auto_ptr_ref {
_Tp1* _M_ptr;
auto_ptr_ref(_Tp1* __p) : _M_ptr(__p) {}
};
template <class _Tp> class auto_ptr {
private:
_Tp* _M_ptr;
public:
typedef _Tp element_type;
explicit auto_ptr(_Tp* __p = 0) __STL_NOTHROW : _M_ptr(__p) {}
auto_ptr(auto_ptr& __a) __STL_NOTHROW : _M_ptr(__a.release()) {}
template <class _Tp1> auto_ptr(auto_ptr<_Tp1>& __a) __STL_NOTHROW
: _M_ptr(__a.release()) {}
auto_ptr& operator=(auto_ptr& __a) __STL_NOTHROW {
reset(__a.release());
return *this;
}
template <class _Tp1>
auto_ptr& operator=(auto_ptr<_Tp1>& __a) __STL_NOTHROW {
reset(__a.release());
return *this;
}
~auto_ptr() { delete _M_ptr; }
_Tp& operator*() const __STL_NOTHROW {
return *_M_ptr;
}
_Tp* operator->() const __STL_NOTHROW {
return _M_ptr;
}
_Tp* get() const __STL_NOTHROW {
return _M_ptr;
}
_Tp* release() __STL_NOTHROW {
_Tp* __tmp = _M_ptr;
_M_ptr = 0;
return __tmp;
}
void reset(_Tp* __p = 0) __STL_NOTHROW {
if (__p != _M_ptr) {
delete _M_ptr;
_M_ptr = __p;
}
}
public:
auto_ptr(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW
: _M_ptr(__ref._M_ptr) {}
// !!!!!!!!!!
auto_ptr& operator=(auto_ptr_ref<_Tp> __ref) __STL_NOTHROW {
if (__ref._M_ptr != this->get()) {
delete _M_ptr;
_M_ptr = __ref._M_ptr;
}
return *this;
}
// !!!!!!!!!!
template <class _Tp1> operator auto_ptr_ref<_Tp1>() __STL_NOTHROW
{ return auto_ptr_ref<_Tp>(this->release()); }
template <class _Tp1> operator auto_ptr<_Tp1>() __STL_NOTHROW
{ return auto_ptr<_Tp1>(this->release()); }
};
} // namespace std




Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.
Похожие темы
- Форум Непонятки с auto_ptr (2005)
- Форум Сломали C++23. (UPD: это шланг 17.0.6 корявый) (2024)
- Форум Реализуйте класс SharedPtr (2014)
- Форум Как создать массив в С++ (2022)
- Форум скомпилировать openbox (2008)
- Форум Почему оператор != не генерируется автоматически, если определен == ? (2018)
- Форум почему компилятор не видит member type базового класса? (2015)
- Форум boost::asio вопрос по принципу работы кода из примеров (2016)
- Форум Logger on C++ ... вроде все правильно ... (2005)
- Форум [MSVC 2005]Компилерокапец (2009)