( 14.4.2017)
unique_ptr shared_ptr , , . API, Object*& - (), . ++ 11, . ( * _ptr )
?
auto_ptr, , , , .
, - , google - :
http://www.codingwisdom.com/codingwisdom/2012/09/reference-counted-smart-pointers-are-for-retards.html
, , , , C .
, , svn: https://sourceforge.net/p/testcppreflect/code/HEAD/tree/SmartPtr.h
.
#pragma once
template <class T>
class SmartPtr
{
public:
SmartPtr() : ptr( nullptr ), next( nullptr )
{
}
SmartPtr( T* pt ) : ptr( pt ), next( nullptr )
{
}
SmartPtr( SmartPtr<T>& sp ) : ptr( nullptr ), next( nullptr )
{
operator=(sp);
}
~SmartPtr()
{
release();
}
T*& refptr()
{
release();
return ptr;
}
T* get()
{
return ptr;
}
T* operator->() const
{
return ptr;
}
T* operator=( T* _ptr )
{
release();
ptr = _ptr;
return ptr;
}
SmartPtr<T>& operator=( SmartPtr<T>& sp )
{
release();
ptr = sp.ptr;
if ( ptr )
{
if( sp.next == nullptr )
{
next = &sp;
sp.next = this;
} else {
SmartPtr<T>* it = &sp;
while( it->next != &sp )
it = it->next;
next = &sp;
it->next = this;
}
}
return *this;
}
void release()
{
if ( !ptr )
return;
if( next != nullptr )
{
SmartPtr<T>* it = next;
while( it->next != this )
it = it->next;
if( it == it->next->next )
it->next = nullptr;
else
it->next = next;
next = nullptr;
ptr = nullptr;
return;
}
delete ptr;
ptr = nullptr;
}
T* ptr;
SmartPtr<T>* next;
};
, , , .
* refptr(), . ( "++ jewels" )
, , - , .: -)
.
:
: Smart Pointer int * countPtr ? , , Use-Count ? , ?
, , - ββ . , ( ), - (), .
- , , , , . , . , .
For memory leaks, I recommend finding existing tools and using them - for example, this one:
https://sourceforge.net/projects/diagnostic/
(There are many, but none of them works reliably / well enough).
I know that you want to dislike this implementation, but in reality - please tell me what obstacles you see in this implementation ?!