Smart Pointer Class

DESCRIPTION

This smart pointer package provides 2 main classes for use

1. CountedPtr - Is a reference counted smart pointer that can be used with any type - Allows direct assignment to base class smart pointer by a derived class - Supports static and dynamic casts where they are supported by the underlying types - Uses the copy constructor to clone the pointed to object - Calls the right delete when done with object

2. BackPtr - Is a class for backward references, when the pointer is not owned - Can be assigned a CountedPtr - Can check if the underlying object is still valid - Can get a CountedPtr from a BackPtr if the underlying object is valid, otherwise an exception will be thrown. This prevents the problem of dangling pointers

ENVIRONMENT

This code was developed with VC 6, SP3 However, I tried not to use anything specific to VC and hopefully it will compile using other compilers as well.

This code uses member templates a lot. This might cause problems for some compilers that do not support them.

USE

  • 1. Add the JRB_PTR.CPP file to your project
  • 2. #include "jrb_ptr.h"
  • 3. using namespace jrb_sptr;
  • CountedPtr and BackPtr are templates. Use them like other template classes

    You cannot assign a regular pointer directly to a CountedPtr (and that includes a newly created one with new) to assign use Make_CP as in

    
    CountedPtr tptr = Make_CP(new T);
    
    You can assign a CountedPtr to a BackPtr. To get a CountedPtr from a BackPtr use the GetCountedPtr member function. Notice, if the pointer is no longer valid, it will throw an exception. You might want to us IsValid to check the validity first.

    LIMITATIONS

    1. Not thread safe. This package is not thread safe, nor does it pretend to be. I do not have that much experience with the intricacies of multi-threaded programming, and thus leave it to others who would like to, to make it thread safe

    2. Has not been tested with multiple inheritance (esp virtual base classes). However, since MI is not used with great frequency, I did not think it too much of a problem.

    SAMPLE CODE

    Please see PtrTest.cpp (download below)

    CONCLUSION

    I hope you enjoy using this package. I learned some neat techniques writing it. If you have any questions or comments, ask on them on the message board, and I will try to answer them.

    LICENSING

    // ***********************************
    // Copyright 1999 John R. Bandela
    // This work may be freely used, copied, distributed, and extended, provided
    // the copyright notice above and this statement are maintained at the top of the code

    Please, I am requesting (not requiring), that if derivative works are made of this work, that
    they be posted freely to a board such as this, so that all may benefit.

    Downloads

    Download source code - 7 Kb


    Comments

    • There are no comments yet. Be the first to comment!

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

    • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

    Most Popular Programming Stories

    More for Developers

    RSS Feeds