At game startup I load a bunch of weapon data from an XML file, and keep them in a std::map<int, Weapon*> so I can then get an instance of a weapon looking them up by ID. Note that depending on the data in the file, different subclasses of Weapon will be constructed and stored in the map.

The trouble is, I obviously don't want to have every player in the game using the same instance of a weapon, so I want to use a copy constructor to copy the objects out of the map for use in game e.g.

The idea being that the two player objects should now have identical, but distinct weapons, each initialised from the object in the map. Trouble is, using a copy constructor on Weapon doesn't work as it's an abstract class (contains pure virtual functions).

The obvious solution to this is to load a new object from the XML file each time, and construct the correct subclass, but it would be nice to be able to load from file only once, and then copy the created object.

Anyone got any thoughs? How do I create a copy of a derived class object when I only have access to the base class pointer?

If you're doing this by ID, and all the types are only brought in at load time, then why not just put them in an array or vector? I think constant access time would be much preferable to lg(n) with map.

If that's the case, then I recommend hash_map instead of map, since it will be almost constant time without any requirements in terms of number or continuity. (since the size will be relatively constant, since it should be created at load time, so you won't have to take the hit of re-hashing)