Kernel SDK Developer's Guide > Basic Operations > Working with Smart Pointers > Creating an Empty Implementation of the Reference Counter
Creating an Empty Implementation of the Reference Counter

The OdStaticRxObject template class is used to create classes and their instances that do not count references when a program operates using smart pointers to the instances. This template creates an empty implementation of the addRef() and release() virtual methods of the base class and does not create the reference counter in the class. You can use this template as a wrapper for your classes when you create instances in automatic or static memory, allocate dynamic memory for instances, or derive a new class from the specified class.

If you want to create your own class for static instances, derive your class from the OdRxObject base class using the following class definition:

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

Together with the class definition, declare the smart pointer type to your class using the OdSmartPtr template:

typedef OdSmartPtr<MyObj> MyObjPtr;

To create an instance in automatic or static memory, use the OdStaticRxObject template as the wrapper of your class when you declare the variables:

OdStaticRxObject<MyObj> my;

If you want to use own class for dynamic instances, add the ODRX_HEAP_OPERATORS macro in the class definition to create the standard implementation of the new and delete operators:

class MyObj : public OdRxObject
{
 public:
   ODRX_HEAP_OPERATORS();

 public:
   MyObj();
   ~MyObj();
};

To create an instance in dynamic memory, declare the raw pointer of your class type in the code and use the OdStaticRxObject template in the (new) operator:

MyObj* pMy = new OdStaticRxObject<MyObj>;

If you want to insert smart pointer functionality in all classes derived from your own class, derive your class directly from the OdStaticRxObject template and add the ODRX_HEAP_OPERATORS macro in the class definition:

class MyObj : public OdStaticRxObject<OdRxObject>
{
 public:
   ODRX_HEAP_OPERATORS();

 public:
   MyObj();
   ~MyObj();
};

To create an instance in automatic or static memory, declare a variable for your class type without a template:

MyObj my;

To create an instance in dynamic memory, declare a pointer to your class type and use the (new) operator without a template:

MyObj* pMy = new MyObj;

You can declare the smart pointer and assign to it the address of the static or dynamic instance:

MyObjPtr pSmart0 = &my;
MyObjPtr pSmart1 = pMy;
MyObjPtr pSmart2 = new OdStaticRxObject<MyObj>;
MyObjPtr pSmart3 = new MyObj;

If you created a dynamic instance, you must delete it using the (delete) operator, otherwise memory leaks occur.

delete pMy;

See Also

Overview of Smart Pointer Classes

Using an Empty Implementation of the Reference Counter

Example of Counting References

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