Should the ARG_TYPE for a CArray be const & or not

2019-08-26 09:54发布

问题:

What's the difference between these 2 lines

CArray<MyClass, MyClass> MyArray

CArray<MyClass, const MyClass & > MyArray  

回答1:

The MyClass is always copied to the array. But the first form copies it twice: the first time when its given as a parameter to the Add or SetAt function, the second time internally. With the second form you avoid the first copy.



回答2:

In the first case you need to have accessable copy constructor in MyClass, and MyClass will be passed by value (copied) in some members of CArray( in CArray::Add for instance).