object to pointer to function

Started by
2 comments, last by VISQI 13 years, 9 months ago
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
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 ?
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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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

This topic is closed to new replies.

Advertisement