Simple questions about CArray

1.) What is the difference between

CArray <SomeClass> collection;

      

and

CArray <SomeClass,SomeClass> collection;

      

or even

CArray <SomeClass* ,SomeClass* > collection;

      

?

2.) While reading some of the comments on Stackoverflow, I came up with a note "Don't use CArray". Why is CArray not being used?

+2


a source to share


3 answers


It:

CArray <SomeClass> collection;

      

is equivalent to this:

CArray <SomeClass, const SomeClass&> collection;

      

The second template parameter is used to specify the type of access to the elements. Template parameters are described in the MSDN documentation .



It:

CArray <SomeClass* ,SomeClass* > collection;

      

stores a set of pointers to type objects SomeClass

, while the other two store collections of type objects SomeClass

.

As for why you "shouldn't use it", std::vector

which is part of the C ++ language standard and therefore portable is probably the best choice for most projects. If you have legacy code that uses CArray

, you may need to use it, and there is nothing wrong with that.

+7


a source


The difference is what is stored in the object CArray

and how it is stored in the object, whether the elements are CArray

objects or pointers to objects of some class.

CArray

seems to have a few unexpected actions. It has been around for a long time and is intended to be installed in the MFC ecosystem. The C ++ Standard Template Library vector

has much nicer and more general characteristics, especially when dealing with objects other than simple data types.

My experience was to use CList

, and CArray

with pointers to objects. By doing this, they seem to be more predictable, although you need to worry about memory management.

One of the problems with the source code in afxtempl.h

is that when CArray

incremented by an internal function SetSize()

, the function is memcpy_s()

used to copy items CArray

from one area of ​​memory to another. Therefore, you need to be careful with pointers and shallow copying of elements CArray

. And since it is memcpy_s()

used instead memmove_s()

, if you do something funky with areas of memory with a lot of areas, there might be a problem.



This is probably why my experience with CArray

as a container for pointers to objects works much better.

Interestingly, the Append()

and methods Copy()

use an internal function CopyElements()

that does element-by-element assignment rather than a function call memcpy_s()

. However, these methods are used with objects CArray

, not individual elements.

CArray

derived from CObject

, which means the data structure will have all the luggage CObject

. However, there are also some good things that CObject

serialization brings to the world of MFC.

It looks like you should be using a reference as the second template argument to use CArray <SomeClass, SomeClass &> collection;

if you are using a class. I ran into problems when I didn't, until I discovered the Create Array section from MFC Collection: CArray Class.

0


a source


2) Since CArray reallocates memory when adding a new element.

-2


a source







All Articles