Why the hell does anyone need to know pointers?
quote:Original post by petewood
this is 2 1/2 years old
maybe but it doesn''t make it any less useful does it? I mean they did decide to achieve all of these so I''m glad that someone found it useful; I was afraid they did it just to slow down the forum search
--{You fight like a dairy farmer!}
If you''re making games, pointers will save your life. Here is a quick example. We have player objects, and we have vehicle objects. A player has a pointer to a vehicle. When he issues commands (forward, reverse, headlights on) those commands are just forwarded to whatever vehicle his pointer points to.
Now, we can make a player* called ''myplayer'' to represent us... and 2 vehicle* , we call them ''helicopter'' and ''motorcycle''.
Now, we just call myplayer->SetVehicle(motorcycle) and we are driving the motorcycle... vroom vroom, screeeeech... fun right? Now just call myplayer->SetVehicle(heliocpter) and you are in control of the helicopter... Super Simple, very fast, no excess unneeded copying of structs and shifting of data... just change one little number and you are now flying...
class vehicle{public: void DoCommands(somestruct);}class player{public: player() : m_pMyVehicle(0) {} void SetVehicle(vehicle* veh) { m_pMyVehicle = veh; } void DoCommands() { if(m_pMyVehicle) { m_pMyVehicle->DoCommands(somestruct); } }private: vehicle* m_pMyVehicle;}
Now, we can make a player* called ''myplayer'' to represent us... and 2 vehicle* , we call them ''helicopter'' and ''motorcycle''.
Now, we just call myplayer->SetVehicle(motorcycle) and we are driving the motorcycle... vroom vroom, screeeeech... fun right? Now just call myplayer->SetVehicle(heliocpter) and you are in control of the helicopter... Super Simple, very fast, no excess unneeded copying of structs and shifting of data... just change one little number and you are now flying...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement