RefCounts

  • Thread starter news.tkdsoftware.com
  • Start date
N

news.tkdsoftware.com

Could someone illustrate how I could implement reference counting within my
base class which all my classes are derived from so that all my objects are
reference counted. This way once I create my classes and all their
references have been released, the object is then deleted instead of me
having to keep up with whether it has 0 references or not?

Thanks in advance!
Chris

--
 
D

David Hilsee

news.tkdsoftware.com said:
Could someone illustrate how I could implement reference counting within my
base class which all my classes are derived from so that all my objects are
reference counted. This way once I create my classes and all their
references have been released, the object is then deleted instead of me
having to keep up with whether it has 0 references or not?

The FAQ (http://www.parashift.com/c++-faq-lite/) contains a good example of
a reference-counted pointer implementation under section 16 ("Freestore
management") questions 21 ("How do I do simple reference counting?") on. If
you inherit from the class that the FAQ calls Fred, you should be able to
use "FredPtr" for reference counting instances of the derived class, too.
Don't forget the virtual destructor, of course. Also, instead of writing
your reference counting own code, consider using something like
boost::shared_ptr.
 
J

Jonathan Turkanis

David Hilsee said:
Don't forget the virtual destructor, of course. Also, instead of writing
your reference counting own code, consider using something like
boost::shared_ptr.

Good advice. You can also reuse boost::detail::shared_count, which is what I
happen to be doing right now ;-)

Jonathan
 
D

David Hilsee

Jonathan Turkanis said:
Good advice. You can also reuse boost::detail::shared_count, which is what I
happen to be doing right now ;-)

Out of curiosity, why did you choose to use shared_count and not shared_ptr?
 
J

Jonathan Turkanis

David Hilsee said:
Out of curiosity, why did you choose to use shared_count and not shared_ptr?

I'm writing a reference-counted shared pointer with slightly diferent semantics
than shared_ptr. It's part of an aspect-oriented programming library I'm working
on with Chrisopher Diggins. We're planning a preliminary announcement soon.

Jonathan
 
S

Stewart Gordon

news.tkdsoftware.com said:
Could someone illustrate how I could implement reference counting within my
base class which all my classes are derived from so that all my objects are
reference counted. This way once I create my classes and all their
references have been released, the object is then deleted instead of me
having to keep up with whether it has 0 references or not?

I've just looked at the FAQ example, and it looks neat.

I did something similar a while ago to implement reference-counted
vectors. The two classes were called Vector and VectorData. The idea
was that the class user need only concern him/herself with Vector, as it
wrapped the various VectorData methods to provide functionality.

Don't forget that reference counting won't work if there's any chance of
circular references in your data structure.

Stewart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,131
Latest member
IsiahLiebe
Top