Board index » delphi » Help: class method not calling correct descendent Create method

Help: class method not calling correct descendent Create method

I've got a base class defined:

        TBaseClass = class of TBase;
        TBase = class
                ...
        public
                constructor Create;
                class procedure New(...);
                ...
        end;

and several derived classes:

        TDerivedA = class(TBase)
                ...
        public
                constructor Create; override;
                ...
        end;

        TDerivedB = class(TBase)
                ...
        public
                constructor Create; override;
        end;

Note that none of the derived classes have overriden the "New" method, which
is defined as:

        class procedure TBase.New(...);
        var
                NewDerived: TBaseClass;
        begin
                NewDerived := Self.Create;
                ...
        end;

When I call, for example, TDerivedA.New(...), the TBase.New method is called
and the _class reference_ Self correctly shows a class type of TDerivedA.  
However, Self.Create doesn't call TDerivedA.Create, rather TBase.Create is
called.

Anyone know why this isn't working?  Incidently I (successfully) did something
quite similar to this in Delphi 1.0, where my TBase class was derived from
TForm.  Then given a string, S,  which was the class name of a derived class,
I did something akin to:

        var
                ClassRef: TPersistentClass;
                NewDerived: TBase;
        begin
                ClassRef := GetClass(S); {'TDerivedA', for example)
                NewDerived := TBaseClass(ClassRef).Create(...);
                ...
        end;

Any ideas would be appreciated.

TIA,
Gerard

Gerard M. Averill
Assistant Researcher

Center for Health Systems Research and Analysis
University of Wisconsin, Madison
E-mail: gaver...@chsra.wisc.edu

 

Re:Help: class method not calling correct descendent Create method


Never mind....  As it turned out, my derived Create methods were not declared
with "override", nor my base class Create with "virtual".  Adding these
directives solved the problem. ;)

G.

Gerard M. Averill
Assistant Researcher

Center for Health Systems Research and Analysis
University of Wisconsin, Madison
E-mail: gaver...@chsra.wisc.edu

Re:Help: class method not calling correct descendent Create method


Quote
Gerard M. Averill wrote:

> I've got a base class defined:

>         TBaseClass = class of TBase;
>         TBase = class
>                 ...
>         public
>                 constructor Create;
>                 class procedure New(...);
>                 ...
>         end;

> and several derived classes:

>         TDerivedA = class(TBase)
>                 ...
>         public
>                 constructor Create; override;
>                 ...
>         end;

Should your base class constructor not be 'virtual' ?

--
Jolyon Smith                         |  The views represented above are my own
Application Development Consultant   |  and do not necessarily represent the
PLATINUM Solutions (UK)              |  views of my employer.

Other Threads