Mail Archives: djgpp/1997/03/28/23:30:46
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)
- Raw text -