Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

delete operator on smart pointers.

 
 
=?iso-8859-1?q?Ernesto_Basc=F3n?=
Guest
Posts: n/a
 
      01-06-2007
I am implementing my custom smart pointer:

template <typename T>
class MySmartPtr
{
public:
MySmartPtr(T* aPointer)
{
mPointer = aPointer;
}

inline T* operator->() const {return mPointer; }

private:
T* mPointer;
};


It could be used in this way:

MySmartPtr<A> obj = new A();
obj->Method1();
int value = obj->Method2();

The operator-> overload allows accessing the A methods as they are
accessed via a common pointer.

I want to allow the final user call the delete operator as in a common
pointer when he wants to explicitly remove the pointer:

delete obj;


There is a way I could implement an overload of the delete pointer in
my MySmartPtr<T> or there is some workaround on this?

Thanks in advance,


ernesto

 
Reply With Quote
 
 
 
 
Heinz Ozwirk
Guest
Posts: n/a
 
      01-07-2007
"Ernesto Bascón" <> schrieb im Newsbeitrag
news: ups.com...
>I am implementing my custom smart pointer:
>
> template <typename T>
> class MySmartPtr
> {
> public:
> MySmartPtr(T* aPointer)
> {
> mPointer = aPointer;
> }
>
> inline T* operator->() const {return mPointer; }
>
> private:
> T* mPointer;
> };
>
>
> It could be used in this way:
>
> MySmartPtr<A> obj = new A();
> obj->Method1();
> int value = obj->Method2();
>
> The operator-> overload allows accessing the A methods as they are
> accessed via a common pointer.
>
> I want to allow the final user call the delete operator as in a common
> pointer when he wants to explicitly remove the pointer:
>
> delete obj;
>
>
> There is a way I could implement an overload of the delete pointer in
> my MySmartPtr<T> or there is some workaround on this?


To make your smart pointer really smart, it should also implement a default
constructor, a copy constructor and an assignment operator (actually two, a
copy assignment operator and one to assign raw pointers). If they are
implemented correctly, you could just write

obj = 0; // or NULL

to free whatever the smartpointer owned before.

HTH
Heinz


 
Reply With Quote
 
 
 
 
Paul Hosre
Guest
Posts: n/a
 
      01-07-2007

Ernesto Bascón wrote:
> I am implementing my custom smart pointer:
>
> template <typename T>
> class MySmartPtr
> {
> public:
> MySmartPtr(T* aPointer)
> {
> mPointer = aPointer;
> }
>
> inline T* operator->() const {return mPointer; }
>
> private:
> T* mPointer;
> };
>
>
> It could be used in this way:
>
> MySmartPtr<A> obj = new A();
> obj->Method1();
> int value = obj->Method2();
>
> The operator-> overload allows accessing the A methods as they are
> accessed via a common pointer.
>
> I want to allow the final user call the delete operator as in a common
> pointer when he wants to explicitly remove the pointer:
>
> delete obj;
>
>
> There is a way I could implement an overload of the delete pointer in
> my MySmartPtr<T> or there is some workaround on this?
>
> Thanks in advance,
>
>
> ernesto


did you try debuggig and watch what object is actually in use then
delete it directly?

 
Reply With Quote
 
Paul Hosre
Guest
Posts: n/a
 
      01-07-2007

Heinz Ozwirk wrote:

>
> To make your smart pointer really smart, it should also implement a default
> constructor, a copy constructor and an assignment operator (actually two, a
> copy assignment operator and one to assign raw pointers). If they are
> implemented correctly, you could just write
>
> obj = 0; // or NULL
>
> to free whatever the smartpointer owned before.
>
> HTH
> Heinz

That's an elegant point!
I watched my google search carefully with my pair of glasses and I
turned out to have the same incorrect thought with others. hehe

 
Reply With Quote
 
Paul Hosre
Guest
Posts: n/a
 
      01-07-2007

Heinz Ozwirk wrote:

>
> To make your smart pointer really smart, it should also implement a default
> constructor, a copy constructor and an assignment operator (actually two, a
> copy assignment operator and one to assign raw pointers). If they are
> implemented correctly, you could just write
>
> obj = 0; // or NULL
>
> to free whatever the smartpointer owned before.
>
> HTH
> Heinz

That's an elegant point!
I watched my google search carefully with my pair of glasses and I
turned out to have the same incorrect thought with others. heh

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-12-2007
Ernesto Bascón wrote:

> MySmartPtr(T* aPointer){ mPointer = aPointer; }

Better do
MySmartPtr( T *const aPointer=0 )throw():mPointer(aPointer){ }

> MySmartPtr<A> obj = new A();

It can produce sequence

MySmartPtr<A>() + MySmartPtr<A>() + MySmartPtr<A>(const MySmartPtr<A>&)
or MySmartPtr<A>:perator = (const MySmartPtr<A>&)

Better do
MySmartPtr<A> obj (new A() );

> obj->Method1();

Define both
? T* operator-> () ? throw(){ return mPointer; }
? T& operator* () ? throw(){ return *mPointer; }
be shure "const" instead "?" is correct for MySmartPtr behaviour

> int value = obj->Method2();


> I want to allow the final user call the delete operator as in a common
> pointer when he wants to explicitly remove the pointer:
>
> delete obj;


Try do
public:
operator T* ()throw(){ T *const tmp=mPointer; mPointer=0; return
tmp; }
~MySmartPtr()throw(){ delete mPointer; mPointer=0; }

Define (if you can not dynamic copy like this, else as need)
private:
MySmartPtr(const MySmartPtr&)throw():mPointer(0){ exit(1); }
void operator= (const MySmartPtr&)throw(){ exit(1); }

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-12-2007
Grizlyk wrote:

> Define (if you can not dynamic copy like this, else as need)
> private:
> MySmartPtr(const MySmartPtr&)throw():mPointer(0){ exit(1); }
> void operator= (const MySmartPtr&)throw(){ exit(1); }


Yes, and yet
public:
T* operator= (T *const aPointer)throw() { delete mPointer;
mPointer=aPointer; }

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      01-12-2007
Ernesto Bascón wrote:
> I am implementing my custom smart pointer:
>
> template <typename T>
> class MySmartPtr
> {
> public:
> MySmartPtr(T* aPointer)
> {
> mPointer = aPointer;
> }
>
> inline T* operator->() const {return mPointer; }
>
> private:
> T* mPointer;
> };
>
>
> It could be used in this way:
>
> MySmartPtr<A> obj = new A();
> obj->Method1();
> int value = obj->Method2();
>
> The operator-> overload allows accessing the A methods as they are
> accessed via a common pointer.
>
> I want to allow the final user call the delete operator as in a common
> pointer when he wants to explicitly remove the pointer:
>
> delete obj;
>
>
> There is a way I could implement an overload of the delete pointer in
> my MySmartPtr<T> or there is some workaround on this?


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?

As for overloading delete, you probably don't want to do that because
it would change the semantics of delete from operating on the object
(viz. MySmartPtr<>) to operating on the contained object. In other
words, class-specific new/delete operators are for deleting the smart
pointer class itself:

MySmartPtr<T>* pT
= new MySmartPtr<T>( 0 ); // Calls MySmartPtr<T>:perator new
delete pT; // Calls MySmartPtr<T>:perator delete

You could accomplish your goal by supplying a cast to T*, but that
allows some other dangerous code. The preferred method is to supply a
reset function:

template<class T>
void MySmartPtr<T>::Reset( T* const p = 0 )
{
delete mPointer;
mPointer = p;
}

It's best to use RAII and let the smart pointer do the dirty work. See
the FAQ mentioned above and those following, and see this chapter from
_Modern C++ Design_ on Loki's smart pointers:

http://www.informit.com/articles/pri...p?p=25264&rl=1

Cheers! --M

 
Reply With Quote
 
=?iso-8859-1?q?Ernesto_Basc=F3n?=
Guest
Posts: n/a
 
      01-12-2007

> 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).

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      01-12-2007

Ernesto Bascón wrote:

> I want to allow the final user call the delete operator as in a common
> pointer when he wants to explicitly remove the pointer:
>
> delete obj;


No you don't.

 
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