Okay, I'm making a shooter game, and all my weapons are differnt userdefined objects; ie. the flamethrower works differently then the gun, however, I'm calling some functions in my "main", an example of this would be;

/* <Init> */
MachineGunClass *CurrentWeapon;
MachineGunClass *MachineGun;
MachineGun = new MachineGunClass();

CurrentWeapon = MachineGun;
/* </Init> */

/* <Usage> */
/* Lots of code */
CurrentWeapon->Shoot();
/*More code */
/* </Usage> */

So this works as it should, however let's say I wanted to use my flamethrower, to change to that weapon I would call the following;

FlameThrowerClass *FlameThrower;
FlameThrower = new FlameThrowerClass();
CurrentWeapon = FlameThrower;

However this ofc, wont work, but wouldn't it work if I changed CurrentWeapon to a void pointer, and then typecasted that, on each weapon change? - Wouldn't that be possible, and really is there any good guides on typecasting?

Thanks in advance!

Dani AI

Generated

Short answer: avoid a void* + reinterpret_cast for this. As suspected, you want polymorphism — and were right to push a common interface. Using a base type lets you call the same operations (fire, reload, etc.) and get the correct behavior for each weapon without unsafe casts.

Design sketch: expose only the common surface in an abstract base (pure virtuals if each weapon must implement them), and give the base a virtual destructor so deletion through the base is safe. Manage lifetime with RAII (prefer std::unique_ptr) so switching weapons is simple and leak-free. If you ever need a derived-only API, use dynamic_cast and check for null rather than reinterpret_cast.

Example (minimal, avoids the forum's earlier snippets):

struct IWeapon {
    virtual void fire() = 0;
    virtual int ammo() const = 0;
    virtual ~IWeapon() = default;
};

struct Flame : IWeapon {
    void fire() override { /* call particle system */ }
    int ammo() const override { return 100; }
};

#include <memory>
std::unique_ptr<IWeapon> current = std::make_unique<Flame>();
current->fire(); // dispatches to Flame::fire()

// optional safe downcast
if (auto *f = dynamic_cast<Flame*>(current.get())) {
    // flame-specific calls
}

Quick troubleshooting notes:

  • Never use void*/reinterpret_cast for polymorphism — it discards type info and can cause undefined behavior.
  • Ensure the base has a virtual destructor or deleting derived via base pointer is UB.
  • Avoid object slicing by storing pointers/references, not by-value objects.
  • dynamic_cast is the safe way to downcast (requires RTTI); it costs a bit at runtime but prevents crashes.

This pattern keeps the code clear, type-safe, and easy to extend as you add more weapon types.

Recommended Answers

All 8 Replies

I'm not sure about making it a void pointer but you should be able to make a weapons class that is the bass for all of your specific classes and then make a pointer of the base class and re assign it with your specialized class pointer. this would be more inline with c++ OOP style.

class Weapon
{
       // all standard functions and variables.
}

class Flamethrower : public Weapon
{
       //  overide all functions from wepon that are specific to a flamethrower.
}

int main()
{
       Weapon * weapon;
       // later on
       Flamethrower * temp = new Flamethrower;
       weapon = temp;
       // ...
}

this will require the use of virtual functions and may be a little more than you are looking for but i believe this will work.

Smarty, a simple workaround using inherence, well guess it could work, but kinda hoped, for the ability to do something like:

(reinterpret_cast<FlameThrowerClass*>(CurrentWeapon));
// or (reinterpret_cast<MachineGunClass*>(CurrentWeapon));

//Setting the CurrentWeapon to the address of the specific class and then:

CurrentWeapon->Shoot();

Will use your solution as a backup, if noone helps me out, in the manner i wants, (+rep 4 u)

Will setting these equal to each worker workout without operator overloading?

yeah your setting a pointer equal to another. classes don't need an operator=() function for that its handled by the compiler.

>> the flamethrower works differently then the gun

Does it?

The flamethrower shoots and so does the gun.
The flamethrower has some mass and so does the gun
The flamethrower can run empty and so does the gun.

The point here is that, they do differ internally, but not so much
externally. Thus a solution to your problem is to create a hierarchy
with virtual and pure virtual functions, as pointed out already, while
making the have the same functions like shoot() or destroy(), or whatever.

yeah your setting a pointer equal to another. classes don't need an operator=() function for that its handled by the compiler.

Fantastic, just wondered, could be, that I had to overload the = operator for each class I wanted to input to my "WeaponMasterClass", however I guess I doesn't then, and thats just super! :)

>> the flamethrower works differently then the gun

Does it?

The flamethrower shoots and so does the gun.
The flamethrower has some mass and so does the gun
The flamethrower can run empty and so does the gun.

The point here is that, they do differ internally, but not so much
externally. Thus a solution to your problem is to create a hierarchy
with virtual and pure virtual functions, as pointed out already, while
making the have the same functions like shoot() or destroy(), or whatever.

You are absolutely right, alle weapons got;

  • Shoot
  • Reload
  • ReloadTime
  • IsRealoading
  • Fireing Speed
  • ect.

However, the flamethrower works in a totally differentmanner, ie. calling my particle engine, while my MachineGunClass, simply calls a glPoint.

Just to be sure before, I'm marking this as solved, then I would just make a class like this, right?

class WeaponMasterClass
{
virtual Shoot();
virtual Reload();
virtual BulletsLeftInClip();
virtual IsReloading();
// virtual vars I need, ect.
};

And then calling this, with it set equal to some other weaponobject, lets say the flamethrower, would make it use the flamethrowers shoot, instead, since it's virtual?

P.S.: Virtual, vs. Pure Virtual?

>>However, the flamethrower works in a totally differentmanner, ie. calling my particle engine, while my MachineGunClass, simply calls a glPoint.

Yes I also mentioned that here in my previous post, "The point here is that, they do differ internally, but not so much externally".

class WeaponMasterClass
{
virtual Shoot();
virtual Reload();
virtual BulletsLeftInClip();
virtual IsReloading();
// virtual vars I need, ect.
};

>>And then calling this, with it set equal to some other weaponobject, lets say the flamethrower, would make it use the flamethrowers shoot, instead, since it's virtual?

Yes, if the flamethrower object is derived from that base class.
Not exactly, you will need its return type as well.

>>P.S.: Virtual, vs. Pure Virtual

Virtual and not Pure Virtual.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.