Should the ARG_TYPE for a CArray be const & or not

2019-08-26 09:52发布

What's the difference between these 2 lines

CArray<MyClass, MyClass> MyArray

CArray<MyClass, const MyClass & > MyArray  

2条回答
不美不萌又怎样
2楼-- · 2019-08-26 10:41

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.

查看更多
Juvenile、少年°
3楼-- · 2019-08-26 10:48

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).

查看更多
登录 后发表回答