clone factory


Clone Factory

A factory creates objects of a certain type. The factory takes an ID of some sort as an argument and creates the object which has been registered with the factory with the respective ID.

This code sample shows one way to make  a factory. All objects are stored in an interal (intrusive) list inside the factory together with their ID. To create an object given its ID, the factory searches the list for the ID and calls the copy constructor of the object to create a new object of the same type.

In the moment the new object comes into live, a so called virtual constructor call is made. The virtual constructor principle simply states, that there shall be a function like create() or construct() or init() which shall be called polymorphically. Real constructors may not be virtual, you see. This is because the object hasn´t been created yet and therefore no v-table exists.

The virtual constructor call may be used e.g. for a deserialization call, in case your factory creates message objects. In this case the virtual constructor reads a byte stream and fill the freshly created object with data.

Leave a comment

Your email address will not be published. Required fields are marked *