Jump to content
  • Advertisement
Sign in to follow this  
VISQI

object to pointer to function

This topic is 2899 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey
is there a way to display the pointer's function in an object
EX:i got a PLAYER class(the object is m_player) that has a pointer to WEAPON(the pointer is m_weapon) and i made it public so i can access its functions(like the attack function), how can i do that, is it like: m_player.m_weapons->attack().
btw: that form didn't work, and it is too hard to make a copy constructor because WEAPON is an abstract class and it has TOO many inherited classes inside
appreciated

Share this post


Link to post
Share on other sites
Advertisement
What do you mean that form didn't work ? Did you receive an error message ? Can you post some code, or at least give us more information ?

Share this post


Link to post
Share on other sites
Thats bad encapsulation you should give the player class a function like useWeapon instead. This will make it easier to change the way a player uses a weapon without having to change the game code when a player uses a weapon

When you start making member variables public there is something wrong with your class design (barring very small classes).

Now in the useWeapon function you should write the following


void useWeapon()
{
if (0 != m_weapon) //At least do an assert here
{
m_weapon->attack();
}
}



To come back why your code isn't working could be because your m_player class is a pointer, or m_weapon is null and then the whole app will crash.

Share this post


Link to post
Share on other sites
it wasn't NULL or anything but the problem was that i was using vc++ 2010 express and it highlights the error before execution and i didn't put the m_weapon pointer to point at anything till now
but the useWeapon function just saved my life. i dont know how i missed this. Thanks a million man

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!