Kernel SDK Developer's Guide > Basic Operations > Working with Smart Pointers > Example of a Non-Typified Smart Pointer
Example of a Non-Typified Smart Pointer

The OdRxObjectPtr class is a non-typified smart pointer that works with any classes derived from the OdRxObject. You do not need to define a new type for it. For example, the class definition:

class MyObj : public OdRxObject
{
 public:
   MyObj();
   ~MyObj();
};

The testing function has the following implementation:

void Test()
{
  OdRxObjectPtr pSmartRx0 = OdRxObjectImpl<MyObj>::createObject();

  OdRxObject* pObjRx = pSmartRx0.get();
  odPrintConsoleString(L"\nRefs=%d", pObjRx->numRefs());                 // 1

  OdRxObjectPtr pSmartRx1 = pSmartRx0;                                   // = pObjRx
  OdRxObjectPtr pSmartRx2 = pSmartRx1;                                   // = pObjRx
  odPrintConsoleString(L"\nRefs=%d", pObjRx->numRefs());                 // 3

  OdRxObjectPtr pSmartRx3;                                               // = null

  odPrintConsoleString(L"\n(Rx1==Null)?%d", pSmartRx1.isNull());         // 0 (false)
  odPrintConsoleString(L"\n(Rx3==Null)?%d", pSmartRx3.isNull());         // 1 (true)

  pSmartRx3.attach(pObjRx)                                               // = pObjRx
  odPrintConsoleString(L"\nRefs=%d", pObjRx->numRefs());                 // 3

  OdRxObjectImpl<MyObj> my;

  pSmartRx3.attach(&my)                                                  // = my
  odPrintConsoleString(L"\nRefs=%d", pObjRx->numRefs());                 // 2

  odPrintConsoleString(L"\n(Rx1==Rx2)?%d", (pSmartRx1 == pSmartRx2));    // 1 (true)
  odPrintConsoleString(L"\n(Rx1==Rx3)?%d", (pSmartRx1 == pSmartRx3));    // 0 (false)

  pSmartRx2.detach()                                                     // = null
  pSmartRx3.detach()                                                     // = null
  odPrintConsoleString(L"\nRefs=%d", pObjRx->numRefs());                 // 2

  pSmartRx1.release();                                                   // = null
  odPrintConsoleString(L"\nRefs=%d", pObjRx->numRefs());                 // 1

  pSmartRx0.release();                                                   // 0 - object is destroyed
}

See Also

Working with Smart Pointers

Implementing Smart Pointers

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.