User Documentation
RAVL, Recognition And Vision Library
Ravl - Introduction - Reference Counting - Notes

Big and small objects

When should I make my class a big object class? Why?

(from an email from Charles Galambos)

Basic Reasons:

  1. It's a way of managing memory, freeing it only when all pointers are removed. In this role it avoids a lot of the problems with accessing freed memory and memory leaks.
  2. Like using pointers - it allows you to pass around large objects as a pointer instead of doing lots of copying.
  3. There are also some side benefits when overriding operators on a class. It allows you to define arithmetic operators on your class which is dangerous or impossible with pointers as it conflicts with the build in type operators.
  4. It gives a clear boundary between the implementation of the class's functionality and the interface the user is expected to use. Though only a conceptual advantage, it helps significantly in larger programs.
This means:
  • You can return large allocated objects from functions.
  • You need never see a pointer in your high level code. (Though in implementing containers etc. they still have a place.)
  • When implementing containers etc. you can assume objects assignments are cheap and trivial. This make their implementation a lot cleaner and easier to understand.
  • It gives a clear framework for implementing advanced C++ constructs like virtual constructors.

Hints and tips:

  • If you define a new object that only contains one other big object then you can write all the functionality into the handle and do without the body class. (I.e. create it as a small object, not a big one.)
  • If you have several member objects which are all big then you can do without a body class only if there is no way to assign directly to any of these objects. This avoids `schizophrenic' classes where different handles have different combinations of member data.
  • Never return a handle to a local instance of a big object as a reference: this can seriously break the code used to implement big objects and cause all sorts of problems.
  • Never use pointers to handles - it can get very confusing and is not needed.
  • When writing a big objects all virtual functions should be in the body class, not the handle class. This is a common mistake for beginners and can lead to some very strange behaviour.

Writing big objects:

The easiest way to write a big object is to derive the body from RCBodyC and then the handle for RCHandleC. This automatically provides all the functionality you need.

Bill Christmas
Last modified: Tue Apr 10 14:06:41 BST 2001
Documentation by CxxDoc: Tue Aug 13 10:00:52 2002