C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Smart pointer vs handle class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
newbiecpp
Guest





PostPosted: Sat Oct 23, 2004 2:20 pm    Post subject: Smart pointer vs handle class Reply with quote



After study smart pointer and handle class idioms, I have a few questions.
In smart pointer, you forward all the operations to an object which is
pointed by the smart pointer. In handle class, you copy interface of the
object into handle class and put some code there then forward operations to
the object. Andrew Koenig prefers handle class idiom over smart pointer
idiom in his book. But I don't understand why? To me, smart pointer idiom
looks simpler. In handle class idiom, you have to write lots of code there.
Do I miss something? Thanks for your opinions.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kwikius
Guest





PostPosted: Mon Oct 25, 2004 1:39 pm    Post subject: Re: Smart pointer vs handle class Reply with quote



"newbiecpp" <newbiecpp (AT) yahoo (DOT) com> wrote

Quote:
After study smart pointer and handle class idioms, I have a few
questions.
In smart pointer, you forward all the operations to an object which is
pointed by the smart pointer. In handle class, you copy interface of
the
object into handle class and put some code there then forward
operations to
the object. Andrew Koenig prefers handle class idiom over smart
pointer
idiom in his book. But I don't understand why? To me, smart pointer
idiom
looks simpler. In handle class idiom, you have to write lots of code
there.
Do I miss something? Thanks for your opinions.

A handle is more flexible. You can change the actual shared object,
while references to it remain valid. Useful eg if you want to convert
an object to an 'active' state , by replacing (say) a dumb object by a
smart one. Also where you may be swapping data in and out of memory
etc, when the actual address will change.

Downside is that it is usually slower than a smart-pointer, as you
need to map the handle somehow.

regards
Andy Little

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue Oct 26, 2004 4:20 am    Post subject: Re: Smart pointer vs handle class Reply with quote



"newbiecpp" <newbiecpp (AT) yahoo (DOT) com> wrote


Quote:
After study smart pointer and handle class idioms, I have a few
questions. In smart pointer, you forward all the operations to an
object which is pointed by the smart pointer. In handle class, you
copy interface of the object into handle class and put some code there
then forward operations to the object. Andrew Koenig prefers handle
class idiom over smart pointer idiom in his book. But I don't
understand why? To me, smart pointer idiom looks simpler. In handle
class idiom, you have to write lots of code there. Do I miss
something?

The smart pointer idiom is easier to implement. The handle idiom is
easier to (re)use.

My own preference depends a lot on the classes semantics. If the
identity of elements of the class is significant, I'll use the smart
pointer idiom, since this makes it clear to the user that he does have
reference semantics. If the class should appear to have value semantics
(with either deep copy or copy on write), I'll definitely use a handle.
But there are a lot of cases which could go either way.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.