Archived

This topic is now archived and is closed to further replies.

when do you use pointers?

This topic is 5517 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

I know it''s a huge question, and it depends on a whole style of programming and how classes and functions are organised, and of course they are essential in many cases, but when do you use pointers? Obviously in linked lists, or dealing with DirectX libraries there''s no choice in the matter, but when organising game classes and structure, has anyone any golden rules as to when to use, or strictly avoid pointers?

Share this post


Link to post
Share on other sites
well, some internal structures may use pointers.. do you mean raw pointers or what? the * i only use for multiplication actually..

i use smartpointers everywhere, knowing wich one fits my situation i''m very happy with them. no errors anymore in my code due some abused/missused pointer..



"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
Generally, mostly for dynamic memory allocation.

Aside from that, some specific uses not directly concering dynamic memory allocation would be for order of working with objects. For instance, you might have a group of objects and you want to work with them in a particular order that may change at runtime. If you were to put he objects in an array directly and cycle through them, changing the order would envolve having to copy the objects. If you instead used an array of pointers to the objects, you could just rearrange the pointers without having to copy the objects themselves.

Another application would be something like a skeletal system, where each joint is dependent on the location and orientation of a parent bone/joint. Whenever you update a particular joint you have to quickly be able to refer to the parent, so you can keep a pointer to the parent in each joint (there are better ways of doing this, but I think you can understand the situation pretty well). In that sense, you use pointers when you have to refer to data that is potentially being changed, or when multiple objects have to refer to the same object. In those cases it doesn''t make sense to actually work with individual instances directly, and since they are datamembers of a class, you''d generally want to use pointers instead of references so that you can change the value aside from at construction.

Share this post


Link to post
Share on other sites