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!

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.