Simple CArray questions
1.) What is the difference between
CArray <SomeClass> collection;
and
CArray <SomeClass,SomeClass> collection;
or even
CArray <SomeClass* ,SomeClass* > collection;
?
2.) While reading some 开发者_如何转开发comments on Stackoverflow I came to a note saying "Don't use CArray". Why should CArray not be used?
This:
CArray <SomeClass> collection;
is equivalent to this:
CArray <SomeClass, const SomeClass&> collection;
The second template parameter is used to specify the type through which members are accessed. The template parameters are described in the documentation on MSDN.
This:
CArray <SomeClass* ,SomeClass* > collection;
stores a collection of pointers to objects of type SomeClass
, whereas the other two store collections of objects of type SomeClass
.
As for why you "shouldn't use it," std::vector
, which is part of the C++ language standard, and thus portable, is probably a better choice for most projects. If you have legacy code that uses CArray
, then you may need to use it, and there's nothing wrong with that.
The difference is what is stored in the CArray
object and how it is stored in the object, whether the CArray
elements are objects or pointers to objects of some class.
CArray
seems to have a few unexpected behaviors. It has been around for a long time and was designed to fit into the MFC ecosystem. The C++ Standard Template Library vector
has much nicer and more generic characteristics especially when dealing with objects other than simple data types.
My experience has been with using CList
and CArray
with pointers to objects. Doing this they seem to be more predictable though you do need to worry about managing memory.
One issue from looking at the source in afxtempl.h
is that when the CArray
is made larger through the internal function SetSize()
, a memcpy_s()
function is used to copy CArray
elements from one memory area to another. So you need to be careful about pointers and shallow copying of CArray
elements. And since memcpy_s()
is used rather than memmove_s()
if you are doing something funky with over lapped memory areas, there may be an issue.
This may be why my experience with CArray
as a container for pointers to objects seems to work much better.
The interesting thing is it looks like the Append()
and Copy()
methods use an internal function CopyElements()
which performs an element by element assignment and not a memcpy_s()
function call. However these methods are used with CArray
objects and not with the individual elements.
CArray
is derived from CObject
which means that the data structure will have all the baggage of CObject
. However there are also some good things that CObject
brings along in the MFC world like serialization.
It looks like you should use a reference as the second template argument so you should use CArray <SomeClass, SomeClass &> collection;
if you are using a class. I ran into problems when I did not do this until I discovered the section Creating an Array List from MFC Collections: The CArray Class.
2) Since CArray reallocates memory when new element is added.
精彩评论