Jack wrote:
> I am not quite clear about calling the constructor of the based
> class from the constructor of the derived class.
> 
> For example, I should do something like this:
> 
> type
>   TBaseClass = class(TObject)
>   public
>     color: TColor;
>   end;
> 
>   TDerivedClass = class(TBaseClass)
>   constructor Create;
>   destructor Destroy;
> end;
> 
> constructor TDerivedClass.Create;
> begin
>   // is color variable available here?

Yes. Everything is in scope. Also see the following article.

http://www.cs.wisc.edu/~rkennedy/field-defaults

>   inherited;
>   // other initialization steps
> end;
> 
> destructor TDerivedClass.Destroy;
> begin
>   // other clean up steps
>   inherited;
> end;
> 
> However, sometimes I find that not calling inherited constructor
> still works fine. Will this cause any trouble?

It would cause the same amount of trouble as not calling any other 
inherited method. Decide to call an inherited method by the same 
criteria you use when deciding to call any other function or method.

There is nothing special about calling inherited methods. It's just like 
calling any non-virtual method. Control jumps into the method, the 
statements get executes, and control returns to the caller.

> Similarly, will not calling
> the inherited destructor cause any memory leaks?

If the destructor frees any memory, then not calling the destructor will 
cause leaks. You can't know whether a destructor frees any memory 
without looking at the code, or at least reading the class's documentation.

> Also, before the inherited constructor is called, will the memebers
> of the base class be accessible, e.g. the color variable in the above
> code?

I've answered your questions above, but I suspect I haven't answered the 
questions you really meant to ask.

When you call a constructor to create an object, all the memory for that 
object gets allocated and initialized before _any_ line of code runs 
from any of the class's constructors. The object is fully created by the 
time any constructor runs.

Likewise, memory for an object is not freed until the outermost 
destructor finishes running. All the memory inside an object is 
accessible from within any method of the class, including constructor 
and destructor methods.

-- 
Rob
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to