Re: Template collection classes




"marcomb" <marcomb@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:9772B215-BED7-4E85-B55B-8BC712C9D180@xxxxxxxxxxxxxxxx

CArray<CPoint, CPoint&> PointArray;

This hold a collection of CPoint's. Not references to CPoint's or pointers to CPoint's.

PointArray.Add(aPoint);

This makes a copy in the array.

so we pass aPoint by reference, but still we create a copy of it in
PointArray

Yes.

CArray<CPoint, CPoint> PointArray;

I don't think there is any need to do that. The second template argument is to do with how things are referenced; the first argument is the thing stord.


CPoint aPoint = new CPoint(...paramlist...);
...

That should not compile. (new returns a pointer).

CArray<CPoint, CPoint*> PointArray;

I would guess that will probably not work. use

CArray< CPoint, CPoint &>


CPoint aPoint = new CPoint(...paramlist...);

Again new returns a pointer.

PointArray.Add(aPoint);

what's the difference from

CTypePtrArray<CObList,CPoint*> PointArray;

Do you mean CTypedPtrArray ? I am not sure it is legal with a CObList, but CTypePtrArray is used typically with an array of pointers.

CPoint aPoint = new CPoint(...paramlist...);

Again new returns a pointer.

In pracice I'd use std::vector instead of CArray these days. You can store an array of "things" (which need a copy constructor when you add them to the array) or an array of "pointers to things". It's your choice. In either case be careful about scope, and to delete things created with new at the appropriate point in order to avoid memory leaks.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm

.



Relevant Pages

  • Re: list implementation
    ... >I believe the type "list" is implemented as an array of pointers. ... A Python list is sematically/behaviorally defined as a mutable extensible ... the references are arrays of C pointers. ...
    (comp.lang.python)
  • Re: CFO: Why C?
    ... Renaming pointers to ... >>references doesn't change anything fundamental. ... integer from a byte array is another example. ... Even if I did, the compiler would ...
    (comp.os.linux.development.apps)
  • Re: CFO: Why C?
    ... The difference between pointers and references is more than only a rename. ... Consider a loop over ... an array, where in traditional code often a pointer as well as an index (loop ...
    (comp.os.linux.development.apps)
  • Re: changing the value in an Array with C
    ... >> some things I just can't get my head around, notably pointers, references ... >> and passing arrays. ... I have tried various tutorials and online references, ... > third or Nth - object in an array of objects). ...
    (alt.comp.lang.learn.c-cpp)
  • Re: =& when creating new object
    ... $reference point to the same memory location. ... Please read the chapter called "references are not pointers" in the ... should not consider it an address in PHP. ...
    (comp.lang.php)