From: RB on
Hello, I am struggling to learn the concept of template language
and the usage of CArray. I have found the CArray declaration in
the afxtempl.h file and I have managed to compile with no errors
the apparent construction of
CArray(MyStructType);
But I don't really understand what next to do with it.
I am having a mental block with understanding the template
declaration of
template<class TYPE, class ARG_TYPE>
What exactly is this saying ? The constructor is just CArray( );
My compiler docs don't seem to have much on CArray except a listing
of it's class members. I need to find a good (for dummies) example
with comments on the usage of CArray with a struct as the data type.
I feel like if I could just see an example of what it going on with it,
and what incremental steps are necessary to set it up, I could start to
get the concept.


From: Seetharam on
Here is a sample from MSDN:

http://msdn.microsoft.com/en-us/library/dh1a0227(v=VS.80).aspx

Use google :-)

-Seetharam
From: Hector Santos on
Seetharam wrote:

> Here is a sample from MSDN:
>
> http://msdn.microsoft.com/en-us/library/dh1a0227(v=VS.80).aspx
>
> Use google :-)
>
> -Seetharam


You see, maybe if we started to say:

Use Bing

Or

Bing it!

then maybe Microsoft would change its tune about dropping the
microsoft.* newsgroups. :)

--
HLS
From: Giovanni Dicanio on
"RB" <NoMail(a)NoSpam> wrote:

> Hello, I am struggling to learn the concept of template language
> and the usage of CArray. I have found the CArray declaration in
> the afxtempl.h file and I have managed to compile with no errors
> the apparent construction of

May I suggest you to just go with STL std::vector?
It is more powerful than CArray.
For example, CArray does copies in a "naive" way using memcpy. This works
correctly only for PODs (Plain Ol' Data) but not for more complex classes.
Strange bugs may arise if you use CArray with non-POD classes.
(I recall some of these bugs were discussed in this newsgroup and on some
web forum.)

Moreover, you can easily compose std::vector with other containers (e.g. you
could simply build a vector<vector>).

And vector has a better dynamically-growth policy than CArray (not bad if
you store few items, but can make a difference for millions of items).
CArray uses arithmetic growth, which has a very bad O(N^2) asympotic
complexity; instead vector::push_back increases vector's capacity using a
1.5x factor, offering an amortized O(1) time.


> I am having a mental block with understanding the template
> declaration of
> template<class TYPE, class ARG_TYPE>
> What exactly is this saying ? The constructor is just CArray( );

Note that STL vector in its simplest form has only one template parameter,
i.e. the type you store in the vector, e.g.

vector<int> someIntegers;
vector<MyStruct> collectionOfMyStructs;

No need for the (useless and confusing, IMHO) ARG_TYPE thing.

Using vector is very easy:

#include <vector> // header file for std::vector

// Creates an empty vector storing instances of MyClass
std::vector<MyClass> data;

// Add some stuff
data.push_back( MyClass(...) );
data.push_back( MyClass(...) );
...

Use operator[] to retrieve vector elements (index is 0-based).

And you can use resize() to change vector size.

Other samples on using std::vector and description of its public methods can
be easily found using your favourite search engine.

HTH,
Giovanni


From: Giovanni Dicanio on
"Giovanni Dicanio" <giovanniDOTdicanio(a)REMOVEMEgmail.com> ha scritto nel
messaggio news:#YtSPLf7KHA.1888(a)TK2MSFTNGP05.phx.gbl...

> For example, CArray does copies in a "naive" way using memcpy.

About the (improper) use of memcpy in CArray, it is clearly stated on MSDN
documentation:

CArray Class

http://msdn.microsoft.com/en-us/library/4h2f09ct.aspx

---[begin]---
Most methods that resize a CArray object or add elements to it use memcpy_s
to move elements. This is a problem because memcpy_s is not compatible with
any objects that require the constructor to be called. If the items in the
CArray are not compatible with memcpy_s, you must create a new CArray of the
appropriate size. You must then use CArray::Copy and CArray::SetAt to
populate the new array because those methods use an assignment operator
instead of memcpy_s.
---[end]---


Giovanni