Smart Pointers and other Pointer classes | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 2, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

. 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 .
  • CodeGuru Logo

    CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

    Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

    Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.