C++ Factory Pattern

I came across a question on StackOverflow asking about serialization and object creation based on IDs. Given that the person asking was using a switch statement to create objects, I decided to polish up (read: add comments to) my object factory class that seems to end up in most of my C++ projects one way or another.

It allows simple creation of a object based on a key. For example, the factory object needs to be created with the key and the superclass of all the objects that can be created as template arguments. The the relevant objects need to be registered (Car is a subclass of Vehicle):

Factory<std::string, Vehicle> vehicleFactory;
vehicleFactory.registerClass<Car>("Car");

Then car objects can be created simply by  passing the required key to the createObject function

Car* car =  vehicleFactory.createObject("Car");

 

The factory class works by using a map, which associates a key type with a function to create a object.

#include <map>
/**
* A class for creating objects, with the type of object created based on a key
*
* @param K the key
* @param T the super class that all created classes derive from
*/
template<typename K, typename T>
class Factory {
private:
    typedef T* (*CreateObjectFunc)();

    /**
    * A map keys (K) to functions (CreateObjectFunc)
    * When creating a new type, we simply call the function with the required key
    */
    std::map<K, CreateObjectFunc> mObjectCreator;

    /**
    * Pointers to this function are inserted into the map and called when creating objects
    *
    * @param S the type of class to create
    * @return a object with the type of S
    */
    template<typename S>
    static T* createObject(){
        return new S();
    }
public:

    /**
    * Registers a class to that it can be created via createObject()
    *
    * @param S the class to register, this must be a subclass of T
    * @param id the id to associate with the class. This ID must be unique
    */
    template<typename S>
    void registerClass(K id){
        if (mObjectCreator.find(id) != mObjectCreator.end()){
            //your error handling here
        }        
        mObjectCreator.insert( std::make_pair<K,CreateObjectFunc>(id, &createObject<S> ) );
    }

    /**
    * Returns true if a given key exists
    *
    * @param id the id to check exists
    * @return true if the id exists
    */
    bool hasClass(K id){
        return mObjectCreator.find(id) != mObjectCreator.end();
    }

    /**
    * Creates an object based on an id. It will return null if the key doesn't exist
    *
    * @param id the id of the object to create
    * @return the new object or null if the object id doesn't exist
    */
     T* createObject(K id){
        //Don't use hasClass here as doing so would involve two lookups
        typename std::map<K, CreateObjectFunc>::iterator iter = mObjectCreator.find(id);
        if (iter == mObjectCreator.end()){
            return NULL;
        }
        //calls the required createObject() function
        return ((*iter).second)();
    }
};