Smart Pointers and other Pointer classes

. You can also find this and other articles at his web site at http://www.wizsoft.com/~poul/srs/

Because copy has different meaning with different classes, there are several pointer classes here.

  • Smart pointers classes. Replace the now obsolete CWizPtr, BaseMultiRefPtr, and MultiRefPtr.(I don't remove them for convenience only). No plain C/C++ pointers again!
    File SmartPointers.h defines the following classes:
    • CWizSmartPtr - trivial base class for all others;
    • CWizSafePtr - smart pointer class which basic garbage collections (i.e. knows to
      delete the pointer in destructor). From here and later every pointer class must hold a pointer allocated from heap only! Copy operator attaches the object to the pointer and then detaches it from previous pointer - "pessimistic shadow copy".
    • CWizSafeArray - simple array of objects.
    • CWizDuplPtr - implements deep copy semantics for copy constructor/operator.
      Requires from an object to have a copy constructor. No polymorphism.
    • CWizClonePtr - implements deep copy semantics for copy constructor/operator, with support for polymorphism in pointed object. Requires from an object to have a Clone() function. In the case of polymorphism a Clone() function must be virtual function returning pointer to the root of hierarchy.
      An example:

      class A
      {
      A(const A& a);
      virtual A* Clone() const { return new A(*this); }
      };

      class B
      {
      B(const B& a);
      virtual A* Clone() const { return new B(*this); }
      };

    • CWizBaseMultiRefPtr - Multiply pointer classes with reference counting and polymorphism.
      Class implements basic machinery of reference counting, but doesn't implement a counter.
      Class CWizMultiRefPtr uses int as counter (the simplest solution).
      Implement garbage collection with reference counting, shadow copy, polymorphism.
    • CWizCounterMultiRefPtr - optimized Multiply pointer classes with reference counting and polymorphism. Differs from CWizBaseMultiRefPtr in implementing reference counting,
      which shifted to the managed class itself. It minimizes memory fragmentation, but requires access to the managed classes, which is not always possible.
       

    File SmartPointersSerialize.h defines StoreSmartPtr() and LoadSmartPtr() functions for serializing to/from MFC CArchive.
    File SmartPointerArrays.h defines arrays of smart pointers. All arrays are derived from CArray of MFC.
    File SmartPointerArraysSerialize.h defines template function SerializeArray() for serializing to/from MFC CArchive.
    File SmartPointerArraysSort.h defines template function SerializeArray() for serializing to/from MFC CArchive.
    File SerializeVersion.h defines function SortSmartPrtArray().

  • Multiply pointer class with reference counting CWizMultiPtr.
    Designed to hold objects of one class (no polymorphism). Several pointers can point to the same object; the object deleted when destroying the last pointer to it. By the nature uses shallow copy.
    File MULTIPTR.H .


  • Comments

    • It's all MFC

      Posted by Legacy on 12/18/2001 12:00am

      Originally posted by: NMTop40

      And it doesn't work when I look at it in the window here (no new-lines)

      Reply
    • Error in example?

      Posted by Legacy on 10/24/2000 12:00am

      Originally posted by: Jesper Rug�rd

      Shouldn't B be declared:

      class B : public A
      {...}

      (or something like that?)

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

    Top White Papers and Webcasts

    • On-demand Event Event Date: April 22, 2014 Database professionals -- whether developers or DBAs -- can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this eSeminar with Dell Software's …

    • The exponential growth of data, along with virtualization, is bringing a disruptive level of complexity to your IT infrastructure. Having multiple point solutions for data protection is not the answer, as it adds to the chaos and impedes on your ability to deliver consistent SLAs. Read this white paper to learn how a more holistic view of the infrastructure can help you to unify the data protection schemas by properly evaluating your business needs in order to gain a thorough understanding of the applications …

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds