Board index » delphi » Generic class and Derived creation

Generic class and Derived creation

Heres a short low down on what's wrong. Made a generic class that will never
be used by itself.

TThinker=Class
Public
    Procedure Replicate; //not to be overriden
    Procudre DoAtRep (AThinker : TThinker); Virtual; Abstract;
       //This allows derived class to manipulate the "child" or
"Replication" with out rewriting
        Replicate every time

end;

TDot=Class(TThinker);
     Procedure DoAtRep (AThinker : TThinker);  Override;
end;

Procedure TThinker.Replicate;
Var
  NewThinker :TThinker;
 Begin

  NewThinker :=TThinker.create;
   // do some settings, etc.
    DoAtRep(NewThinker); //pass it on for the derived work
end;

Procedure TDot. DoAtRep (AThinker : TThinker);
Begin
 (AThinker as TDot).drawdots;

//OUCH! This is a big no go, even when the comes from ADot.Create;
ADot.Replicate . (Which calls Adot.DoAtRep)
//Why? Because AThinker was created by TDot.replicate. How do I get TThinker
to create replications of the derived class
//instead of itself. Remember, TThinker was to be a generic class never to
really be used by itself. Class Referencing perhaps, but that lead to
problems.
end;
--
- Tim

 

Re:Generic class and Derived creation


"Tim Chorle"
<timsunive...@mailandnews.com$NOSPAM_REMOVE_EVERYTHING_AFTER_DOLLAR$>
skrev i melding news:88FY5.185323$td5.26462772@news1.rdc2.pa.home.com...

Quote
> Heres a short low down on what's wrong. Made a generic class that will
never
> be used by itself.

> TThinker=Class
> Public
>     Procedure Replicate; file://not to be overriden
>     Procudre DoAtRep (AThinker : TThinker); Virtual; Abstract;
>        file://This allows derived class to manipulate the "child" or
> "Replication" with out rewriting
>         Replicate every time

> end;

> TDot=Class(TThinker);
>      Procedure DoAtRep (AThinker : TThinker);  Override;
> end;

> Procedure TThinker.Replicate;
> Var
>   NewThinker :TThinker;
>  Begin

>   NewThinker :=TThinker.create;
>    // do some settings, etc.
>     DoAtRep(NewThinker); file://pass it on for the derived work
> end;

> Procedure TDot. DoAtRep (AThinker : TThinker);
> Begin
>  (AThinker as TDot).drawdots;

> file://OUCH! This is a big no go, even when the comes from
ADot.Create;
> ADot.Replicate . (Which calls Adot.DoAtRep)
> file://Why? Because AThinker was created by TDot.replicate. How do I
get TThinker
> to create replications of the derived class
> file://instead of itself. Remember, TThinker was to be a generic class
never to
> really be used by itself. Class Referencing perhaps, but that lead to
> problems.
> end;

If I understand you right, Replicate; should create an instance of the
same class as the current instance:

type

  TThinkerClass = Class(TThinker);

Var

  NewThinker :TThinker;
Begin
  NewThinker :=TThinkerClass(Self.ClassType).create;
  DoAtRep(NewThinker);
end;

the TThinkerClass is really only necessary if the TThinker.Create
overrides the TObject.Create;

--
Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Other Threads