array v. CArray/CObjarray

rhilberman

Newcomer
Joined
Apr 1, 2003
Messages
2
I am a beginning programer, with previous C experience. As I am picking up the ropes of C++, I am trying to venture into the realm of programming with objects. Along these lines, I am curious about using CArray or CObjarray instead of the pointer array method that I had used before. I have searched the web, and various books, to find a simple explanation of how to declare a CArray, but everyone seems to have different solutions. What is the best way to turn a statement like:

double *dummy_arr;
int sizeofarray=40;
dummy_arr = (double *) new double*[sizeofarray];

into a Carray statement?

I have seen a solution that includes:
typedef CArray <CRect, CRect&> Crectarray;
Public:
enum{nlinesperpage = 12}
enum{nMax=50}
Crectarray newarr;

crectarray.setsize(nlinesperpage, nMax);
...
The syntax questions that I have with the above statements are abundant! To start with, I am confussed by the CRect& statement (or, as is in the help files, <class TYPE, class ARG_TYPE = const TYPE& >, and I am confussed by how "public:" can be used outside of a class (perhaps the class statement is just missing)?

I am considering just sticking to my old mode of programming, but I also like challenges too much to pass up object oriented programming.!

If someone is willing to give me some pointers, I'd appreciate it.

Thx,
 
If I'm not mistaken, the CArray type is a template class not a true class. It means you have to create your own class based on your datatype - in your case a double. I thought you had to first do something so that the compiler knows you want one based off of the type double. Most of the standard data types have already been implemented as their own types, based on the CArray type. For instance, there is a type called CWordArray.

Check out this link:
http://msdn.microsoft.com/library/d...us/vccore98/HTML/_core_collections_topics.asp

At the bottom is a section of "Further Reading" including a link to making your own type safe collection

I'm far from a C++ programmer though, so this may not be what you want/need :)

-Nerseus
 
Answer -> More questions

Thanks Nerseus,

The links you provided were helpful. One point that I'm still confused about (as if there is only one) is how you turn a CArray into anything other than a vector? From what I can tell, the default is a 1-dimensional array. How do I expand the syntax to incorporate 2 or 3 dimensions?

Cheers,
 
Back
Top