template<class T>
class KLFRefPtr< T >
Stores a pointer to an object with refcount.
This class provides a normal datatype (with default constructor, copy constructor, assignment, ...) that stores a pointer to a structure that provides the functions ref() and deref() to track reference counts.
When this object is copied, or a pointer is assigned, then the pointed object's ref() function is called. When another pointer is assigned, or when this object is destroyed, then the function deref() is called on the previously pointed object, and the object is possibly delete'd
if needed and required.
Automatic object deletion upon zero refcount is optional, see autoDelete() and setAutoDelete(). It is on by default.
When constructed with the default constructor, pointers are initialized to NULL.
The copy constructor and the assignment operator also preserves autodelete setting, eg.
The pointed/referenced object must:
- provide a ref() function (no arguments, return type unimportant)
- provide a deref() function (no arguments). Its return type, when cast into an
int
, is strictly positive as long as the object is still referenced.
Example:
class MyObj {
int refcount;
public:
MyObj(
const QString& s) : name(s), refcount(0) { }
int ref() { return ++refcount; }
int deref() { return --refcount; }
...
QString objname()
const {
return name; }
};
int main() {
ptr = new MyObj("Alice");
ptr = new MyObj("Bob");
ptr2 = NULL;
ptr = obj_random_name();
ptr2 = obj_random_name();
do_something(ptr):
do_something_2(ptr2);
printf("ptr points on object name=%s\n", qPrintable(ptr->objname()));
}
=
QStringList()<<
"Marty"<<
"Jane"<<
"Don"<<
"John"<<
"Phoebe"<<
"Matthew"<<
"Melissa"<<
"Jessica"
return o;
}
void do_something(MyObj *object) { ... }
Definition at line 613 of file klfutil.h.