tags:

views:

75

answers:

3

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?

A: 

Please refer here for information on CArray http://msdn.microsoft.com/en-us/library/4h2f09ct%28VS.80%29.aspx and here for comparison between different MFC collections http://msdn.microsoft.com/en-us/library/y1z022s1%28v=VS.80%29.aspx

Shaji
A: 

2) Since CArray reallocates memory when new element is added.

Ashish
and std::vector?
Andrey
-1: That's not why. vector does the same
John Dibling
+2  A: 

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.

James McNellis