title: Generalizing a reference counting class for use with non-specific protected classes
body: <p>Trying to retro-fit a non-COM class with reference count based protection that prevents its deconstruction when any of its members methods are currently in process.</p>
  
  <p>Wrote a small class designed to call AddRef on construction, and Release on deconstruction; declare an instance upon entry into the protected classes member functions - works OK, but it's bound to protected class which means I can only use it with that class.  </p>
  
  <p>I would like to generalize it so that it can be used with any class. I was thinking maybe templates would help, but I don't know enough about them to write classes that use them.  Here's the class as it exists now, working, but limited to being used within ClsEditWndData instances</p>
  
  <pre><code>#ifndef _ClsProtectMe_h_
  #   define _ClsProtectMe_h_
      typedef void (ClsEditWndData::*pfnREFCOUNTER)();
      class ClsProtectMe
      {
          public:
              ClsProtectMe(                               // constructor
                  pfnREFCOUNTER pfnAddRef,                // pointer to callers' AddRef
                  pfnREFCOUNTER pfnRelease,               // pointer to callers' Release 
                  ClsEditWndData* pThis ) :               // pointer to caller
                      m_pfnAddRef( pfnAddRef ),           // init stored AddRef address
                      m_pfnRelease( pfnRelease ),         // init stored Release address
                      m_pThis( pThis )                    // init stored instance address
              {                                           // ===========================
                  if( m_pThis != NULL )                   // must have non-null instance
                      if( m_pfnRelease != NULL )          // must have non-null release
                          if( m_pfnAddRef != NULL )       // must have non-null addref
                              (m_pThis-&gt;*m_pfnAddRef)();  // call the addref function
              }                                           // ===========================
  
              virtual ~ClsProtectMe(void)                 // destructor
              {                                           // ===========================
                  if( m_pThis != NULL )                   // must have non-null instance
                      if( m_pfnRelease != NULL )          // must have non-null release
                          if( m_pfnAddRef != NULL )       // must have non-null addref
                              (m_pThis-&gt;*m_pfnRelease)(); // call the release function
              }                                           // ===========================
          private:
              pfnREFCOUNTER   m_pfnAddRef;                // stored pointer to AddRef function
              pfnREFCOUNTER   m_pfnRelease;               // stored pointer to Release function
              ClsEditWndData* m_pThis;                    // stored pointer to instance
      };
  #endif
  </code></pre>