RefCounts

Discussion in 'C++' started by news.tkdsoftware.com, Oct 6, 2004.

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

    --
    news.tkdsoftware.com, Oct 6, 2004
    #1
    1. Advertising

  2. news.tkdsoftware.com

    David Hilsee Guest

    "news.tkdsoftware.com" <> wrote in
    message news:...
    > 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.

    --
    David Hilsee
    David Hilsee, Oct 6, 2004
    #2
    1. Advertising

  3. "David Hilsee" <> wrote in message:

    > 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
    Jonathan Turkanis, Oct 7, 2004
    #3
  4. news.tkdsoftware.com

    David Hilsee Guest

    "Jonathan Turkanis" <> wrote in message
    news:...
    >
    > "David Hilsee" <> wrote in message:
    >
    > > 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 ;-)


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

    --
    David Hilsee
    David Hilsee, Oct 7, 2004
    #4
  5. "David Hilsee" <> wrote in message
    news:...
    > "Jonathan Turkanis" <> wrote in message
    > news:...
    > >
    > > "David Hilsee" <> wrote in message:
    > >
    > > > 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 ;-)

    >
    > 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
    Jonathan Turkanis, Oct 7, 2004
    #5
  6. news.tkdsoftware.com wrote:

    > 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.
    Stewart Gordon, Oct 7, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page