Template of pointers or objects?

Started by
4 comments, last by GekkoCube 20 years ago
If you want to use a template linked list, what would be the benefit of making it a template of Object *''s, (object pointers) rather than Object''s (objects)? Where Object can be a class or structure.
Advertisement
One advantage is based on the way objects are stored in the standard library''s container classes. In order to add an item to, for example, a linked list, the code creates a new linked list node, and copy constructs the templated type (for example, Object) from whatever you want to stick in the node. So in order to put something in the list at some point, two copies of the object are going to exist. This isn''t a big deal for ints or doubles, but if the object you want to store in the list is expensive to create or copy then storing pointers may be a better choice.

Also, sometimes you really do want the pointer rather than the object. For example, if all the Objects actually live in array somewhere else, and you want to create a list of the Objects that need updating. In that case storing a copy of the Objects doesn''t make any sense, because updating the copy doesn''t update the original.
And if you forget to write a copy constructor for an object, you''ll get a byte copy, which may not be what you want, depending on what you''re doing.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
I always found that it was easier to get the code right (syntactically and semantically) when using Object *''s in my collections. Even *before* I started using Java.
If your objects are big or complex, you will want to store pointers to them otherwise the copy constructors will do undesired things.

If you are doing something like a dirty-rectangles algorithm where the objects are rectangles which consist *entirely* of four ints and are inherently copyable, you can store the objects by value, that would be ok.

The main drawback I see of storing the objects by value is that you end up calling all sorts of extra code from the copy constructors of any contained objects (by value). Specifically, if you''ve got anything like a boost::smart_ptr inside the objects, there will be an awful lot of reference count incrementing and decrementing goes on.

So I would say

small / simple - use values
big / complicated (specifically constructors / destructors) - use pointers

Of course if you want to use polymorphism you have to use pointers anyway

Mark
I know you said linked list and not dynamic arrays, but just consider:

If you''re using using a list type that stores as a dynamic array instead of an actual linked list, you''ll need to store pointers if you plan on storing any derived types. If derived types have extra data members, their sizes will be different from those of the base class. Operating as if the array was of a derived type could really screw with your data.

This topic is closed to new replies.

Advertisement