[fpc-pascal] class declaration questions
Marc Santhoff
M.Santhoff at t-online.de
Sat Oct 13 01:15:04 CEST 2007
Am Samstag, den 13.10.2007, 00:50 +0200 schrieb Mattias Gaertner:
> On Fri, 12 Oct 2007 18:41:19 -0300
> Joao Morais <post at joaomorais.com.br> wrote:
>
> > Felipe Monteiro de Carvalho wrote:
> > > On 10/12/07, Marc Santhoff <M.Santhoff at t-online.de> wrote:
> > >> Does object pascal allow a class declaration marked as an abstract
> > >> class? Like in java for making sure the class can never be
> > >> instantiated itself, only it's descendants can be?
> > >
> > > The compiler will issue a warning if you instantiate a class with
> > > abstract methods
> >
> > Only if creating an instance straight from the class, eg:
> >
> > This will warn:
> >
> > begin
> > VMyObj := TMyAbstract.Create;
> >
> > This wont:
> >
> > var
> > VMyClass: TMyAbstractClass;
> > begin
> > VMyClass := TMyAbstract;
> > VMyObj := VMyClass.Create;
>
> Correct. The compiler can only warn about compile time errors, not
> runtime errors.
> For runtime you can use
>
> constructor TMyAbstract.Create;
> begin
> if ClassType=TMyAbstract then raise Exception.Create('abstract
> class');
> inherited Create;
> end;
Very nice, exactly what I need in the situation right now. Although a
compiler switch for telling fpc to bark and stop on error when
instantiating abtract classes would be nice I can live with this
solution very well. :)
Thanks for this one and to anyone helping,
Marc
More information about the fpc-pascal
mailing list