 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
newbiecpp Guest
|
Posted: Sat Oct 23, 2004 2:20 pm Post subject: Smart pointer vs handle class |
|
|
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
|
Posted: Mon Oct 25, 2004 1:39 pm Post subject: Re: Smart pointer vs handle class |
|
|
"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
|
Posted: Tue Oct 26, 2004 4:20 am Post subject: Re: Smart pointer vs handle class |
|
|
"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 |
|
 |
|
|
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
|
|