Defining Handle Classes that Don’t Create Bodies
Sometimes you have to leave it up to the user of your handle-body classes to choose a particular body for a given handle. For example, a Shape handle might be able to point to any of the bodies in a shape implementation hierarchy containing triangles, circles, rectangles, and so on. If your application is such that the Shape handle can’t know which concrete shape to use for the body, the user has to decide. One approach is to give the body classes responsibility for creating handles, instead of the other way around as in the first scenario above. The bodies provide a public static make() member function that returns a handle bound to the body containing the make() function.
For the body class:
1. Derive your own class hierarchy from RWBodyBase. Add suffixes, to mark the classes as bodies for use with a handle.
2. Make the body constructors protected.
3. For each constructor, implement an equivalent static make() function that creates an instance but returns a handle to that instance, instead of the instance itself. This ensures that bodies cannot be used independently, but only through the appropriate handle.
For the handle class:
1. Create the handle class by deriving from RWHandleBase.
2. Protect its constructor, so the only way to create a handle is through the body’s static make() functions.
3. Create a conversion constructor that converts a body pointer to a handle instance.
4. Create member functions that forward their calls to the corresponding body implementations.