Re: CList Concept

Tech-Archive recommends: Fix windows errors by optimizing your registry




>> CObject myObject = new CObject();

I have serious doubts the following is compiling
at all. Is it?

I believe you don't need to worry about the list element
destruction in case with CList<CObject,CObject&> myList.
A new instance of object (a copy) is created every time
you insert a new object to the list, and is destroyed
when you remove it from the list. But you have to cope with
releasing the source object. If you use "new", then it
should be a corresponding "delete" somewhere. Or better
allocate instance on the stack, so that it is destroyed
automatically:


CList<CObject,CObject&> myList;
CObject myObject;

myList.AddHead(myObject);


Normally collection templates are used to handle your
own classes, but if you want CObjects, just use CObjList
then.

Alternatively, if you want a list of pointers to
object instances (not a list of object instances),
use CPtrList then. In this case you are responsible
to create and release objects added to the list.



Jimmy Lim wrote:
Hi All,

I have been using C for quite a long time so the object oriented concept is still a bit alien to me. There fore I just want to clarify the concept of the usage of CList.

Below is an example code:
//******************
CList<CObject,CObject&> myList;
CObject myObject = new CObject();

myList.AddHead(myObject);

CObject myObject2 = myList.RemoveHead();
//******************

Questions:
1. do I need to delete myObject after I have added it to myList?
2. if I do it this way, will I get memory leak?

What is the correct usage of CList, because I fear my way of implementation might cause memory leak. I'm coding in a WinCE environment.

Please advice. Thanks

.



Relevant Pages

  • Re: CList Concept
    ... pointer instead of a list of object. ... called automatically by the list class destructor. ... CObject myObject = new CObject; ... if I do it this way, will I get memory leak? ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: CList Concept
    ... there is a bunch of predefined collection classes for standard ... called automatically by the list class destructor. ... CObject myObject = new CObject; ... if I do it this way, will I get memory leak? ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: CList Concept
    ... Thanks for the advice. ... how do I destroy the elements in mylist? ... CObject myObject = new CObject; ... if I do it this way, will I get memory leak? ...
    (microsoft.public.windowsce.embedded.vc)