From: Erik Max Francis Newsgroups: comp.os.msdos.djgpp Subject: Re: newbie questions about C++ Date: Fri, 28 Mar 1997 10:16:03 -0800 Organization: Alcyone Systems Lines: 49 Message-ID: <333C0AE3.5530AE41@alcyone.com> References: <333BE16F DOT 7C80 AT ms11 DOT hinet DOT net> NNTP-Posting-Host: newton.alcyone.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Hilaire Fernandes wrote: > s=new soon(); > f=(father *)s; No cast here is necessary; by the way, a pointer to a derived class can always be implicitly converted to a pointer to the base class without an explicit cast. > So is : > > Q1) delete(s); EQUAL TO delete(f) ?? If they point to the same object (which they do here), yes. > Or should I use instead : > > Q2) free(s); EQUAL TO free(f) ?? No. Always delete objects that you've created with new. malloc and free allocate memory, but do not go through the usual chain of calling constructors at creation and destructors at destruction. > I have this problem because I have a chained list of differents kinds of > objects all herited of a father class but in the list all cast to the > type father. So I don't know any more their original type. That's what inheritance is for. Calling the same virtual function will do the right thing depending on whether the pointer points to an object of the base class or of the derived class. > Q3) I could use a virtual function calling the good destructor for each > kind of class but I'm not sure the destructor free the memory taken by > the new function ?? Huh? Just make your destructor virtual and do the right thing there. (You can make destructors virtual, but not constructors.*) ----- * Actually, there are sneaky little techniques you can use to do this, but that's a whole separate subject. -- Erik Max Francis, &tSftDotIotE / email: max AT alcyone DOT com Alcyone Systems / web: http://www.alcyone.com/max/ San Jose, California, United States / icbm: 37 20 07 N 121 53 38 W \ "I am become death, / destroyer of worlds." / J. Robert Oppenheimer (quoting legend)