The concept is called smart pointers. Google 'smart pointer source' should
return a large heap of references to implementations.
Basically they work by encapsulating a pointer inside a c++ class.
Then you override default constructor, copy constructor,
destructor, assignment operator, constructo, destructor,
assignment operator, copy constructor...
Whenever you assign an object to the smart pointer, a reference counter
for the object gets incremented. When you assign NULL or another object to
the smart pointer, the reference counter gets decremented. If it reaches
zero, the object is deleted.
Whenever you try to test the value of the smart pointer, or access the
referenced object, one of the overriden operators will perform the
corresponding operation as if the smart pointer had been a normal pointer.
Advantages
+ no risc of deleting an object while it is still needed.
+ immediate delete when last reference removed from an object
Disadvantages
- problematic in multi-threaded applications
- requires storage of reference count - either in object or separately
allocated.
- Won't manage pointer rings, which might result in memory leaks.
/Per W
On Fri, 15 Nov 2002, Udaya Prakash wrote:
> Hello,
>
> I am a novice to C++ programming, though I have
> good experience in C programming.
>
> I am looking for some information(A tutorial or an
> article) about "ref/unref mechanism".
>
> Basically what exactly it is? How it is used?
> I only know that it provide a means for the end user
> to instantiate and delete the objects instead of new
> operator or delete object. I also know that it uses
> some kind of reference count on objects (similar to
> linking in Unix).
>
> If there is open source for it, that would be a bonus.
>
> Any suggestions and pointers in this regard would be
> of
> great help to me.
>
> Thank you.
>
> Best Regards,
> Uday.
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Web Hosting - Let the expert host your site
> http://webhosting.yahoo.com
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by: To learn the basics of securing
> your web site with SSL, click here to get a FREE TRIAL of a Thawte
> Server Certificate: http://www.gothawte.com/rd524.html
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|