Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > delete operator on smart pointers.

Reply
Thread Tools

delete operator on smart pointers.

 
 
mlimber
Guest
Posts: n/a
 
      01-13-2007
Ernesto Bascón wrote:
> > I presume there are other functions omitted from your code above, and
> > from your description, it's not clear what semantics you are intending
> > to give this smart pointer (though it sounds like you want shared
> > ownership). Generally, smart pointers (e.g., std::auto_ptr,
> > std::tr1::shared_ptr, boost::scoped_ptr, Loki::SmartPtr, and the smart
> > pointer in FAQ 16.22) are responsible for cleaning up after themselves
> > at the appropriate time so that the user doesn't have worry about
> > deleting anything. Indeed, that's often the primary point. What is your
> > point?

>
> Primarily, I want to provide a hierarchy of smart pointers, providing
> the basic implementation in something like AbstractPtr<T>; the basic
> implementation should include null pointer checking on -> and dangling
> pointer avoiding.
>
> I want to provide also a Ptr<T> : public AbstractPtr<T> implementation
> that should have the same behavior that the standard pointers, but with
> the null pointer checking. This smart pointer should not have any
> semantics (thus, should provide mechanisms to release the pointee
> object manually).


Well, IMHO, its generally bad to have an explicit delete anywhere in
your main code. All resources are best managed by RAII and classes that
enable it (e.g., std::tr1::shared_ptr).

As for the hierarchy, I'm not quite sure why you're bothering. If
you're trying to create a variety of types of smart pointers, check out
Loki's policy-based solution.

As for a class that behaves just like a regular pointer but does
checking, how about something like:

template<class T>
class Ptr
{
T* p_;
public:
Ptr( T* const p=0 ) : p_(p) {}
operator T*() { assert( p_ ); return p_; }
T* operator->() { assert( p_ ); return p_; }
T& operator*() { assert( p_ ); return *p_; }
};

class C {};

Ptr<C> p1( new C );
delete p1;

If you had something grander in mind, I think you could achieve the
same thing (but with considerably more flexibility and code that is
already written and tested) with Loki's smart pointer by creating a
storage policy (let's call it NoDelete) identical to
Loki:efaultSPStorage, except without the delete in its Destroy()
member function:

typedef Loki::SmartPtr<C,
Loki::NoCopy,
Loki::AllowConversion,
Loki::AssertCheck,
NoDelete> MyPtr;

MyPtr p2( new C );
delete p2;

Hope that helps.

Cheers! --M

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
delete MyClass doing more than MyClass::operator delete()? tom C++ 5 07-14-2006 06:21 PM
Smart Card Certificate Logon and Smart Card Wireless EAP-TLS erha Wireless Networking 0 05-19-2005 01:40 AM
How to redefine operator delete/delete[] via macro? Amy C++ 13 02-23-2005 03:36 AM
Mixing new/delete and operator new/delete? Jef Driesen C++ 1 01-19-2005 01:56 PM
trade 64mb smart media for 16mb smart media cards wjva Digital Photography 1 08-20-2003 08:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57