From what I see, you want to write something along these lines:
Code:
// Abstract base ordnance class
class Ordnance {
public:
virtual void initialize(float fXPos, float fYPos, float fAz) = 0;
virtual ~Ordnance() {}
virtual Ordnance* clone() const = 0;
};
//Bolt class - a weapon type
class Bolt : public Ordnance {
public:
//Overriding functions
void initialize(float fXPos, float fYPos, float fAz) {
std::cout << "This is where the program SHOULD go!" << std::endl;
}
Bolt* clone() const {
return new Bolt;
}
};
//Weapon type installed into Turret
class Weapon{
public:
Weapon(const Ordnance* ordnance) : ordnance(ordnance) {}
void fire(float fXPos, float fYPos, float fAz) {
Ordnance* current_ordnance = ordnance->clone();
current_ordnance->initialize(fXPos, fYPos, fAz);
objects.registerNewObject(current_ordnance);
}
private:
Ordnance* ordnance; // Associated ordnance.
};
Note that you should delete current_ordnance or wrap it in a std::auto_ptr if the objects subsystem does not delete it. Alternatively, consider using std::tr1::shared_ptr and thus avoid explicitly having to delete it in both fire() and from the objects subsystem.
Now, from what I see, the Weapon class does not have ownership, in terms of composition, of its Ordnance. Therefore, you can write:
Code:
Bolt plasmaBolt;
Weapon weap(&plasmaBolt);
weap.fire(0.0f, 0.0f, 0.0f);
In case you were not aware, Bolt::clone() overrides Ordnance::clone() even though the return types are different, due to the concept of covariant return types: since a Bolt is-a Ordnance by public inheritance, the overriding clone() virtual function may return a Bolt* instead of an Ordnance*.