tags:

views:

146

answers:

1

Same for CMap, CList, and pretty much everything that uses templates (I guess).

I find it a bit hard to understand when to use which. It's true that for classes and such, the <class, class&> form is usually what you want, but for basic types like int, float, etc., which form is preferred?

+2  A: 

I would say that if you don't really need something else, just use CArray<Type> and ARG_TYPE would be its default const TYPE&. Actually using Type& as ARG_TYPE is not a good idea. This theoretically allows CArray to modify the object/value you passed to the corresponding method. Of course CArray doesn't do anything like this but it is better to be on the safe side.

If you look at the source code that is available within MS VC, you'll see that ARG_TYPE is used in a few methods as type for argument that holds new value for some element(s) of the array such as

void SetAt(INT_PTR nIndex, ARG_TYPE newElement)
INT_PTR Add(ARG_TYPE newElement)
void SetAt(INT_PTR nIndex, ARG_TYPE newElement)
void InsertAt(INT_PTR nIndex, ARG_TYPE newElement, INT_PTR nCount = 1)

If you making your choice between Type and const Type&, then the only thing that is affected is how many times and which data would be copied. When value is passed by reference, only pointer to it is actually passed. This is really important for objects (additional call of copy constructor) but doesn't really matter for simple types. Of course you can try to save a few bytes by forcing copying char or short that is smaller than corresponding pointer (32/64 bit depending on platform) but I don't think that this really worth additional troubles. As I said before, I think that using default CArray<Type> is a good way unless you really have reasons to change it.

iPhone beginner