简体   繁体   中英

What's the difference between CArray<int, int> and CArray<int, int&>?

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?

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.

I often had the same question and am currently trying to work through an odd bug that I believe is related to this difference. I am not certain that I fully understand what is going on yet, but consider this operation:

a.Add(a[0]);

If a is declared as a CArray<int, int&> , then there is a rough case when a is too small to handle the addition and must be grown. SetAtGrow() gets called and makes an enlarged copy of the array. Unfortunately, the reference to a[0] now is a reference to invalid memory since the array has moved.

If a is declared as a CArray<int, int> then a copy of a[0] is passed to the Add function and there is no issue.

Again, I'm still trying to figure this out (which is why I ended up finding this discussion), but there seems to be a subtle issue with using references if you ever reference your own CArray while performing an operation that would increase its size. You can't trust the reference so CArray<int, int> seems preferred.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM