Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-10

class declaration questions

8 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marc Santhoff

Hi,

the reference manual is not explicitly clear in describing class declarations. Is it true for classes as for objects that the first block of components after the class header are visible as if declared public?

E.g.

TAnyClass = class
	fField:anytype;
end;

would make fField visible "public"?

And one other question:
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?

Smth. like:

TAnyClass = abstract class(...)
...
end;

TIA,
Marc

Reply by email

Vincent Snijders
to Marc Santhoff
Marc Santhoff wrote 13 lines
Hi, the reference manual is not explicitly clear in describing class declarations. Is it true for classes as for objects that the first block of components after the class header are visible as if declared public? E.g. TAnyClass = class fField:anytype; end; would make fField visible "public"?

It would be "published".

Show quoted text 11 lines
And one other question: 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? Smth. like: TAnyClass = abstract class(...) ... end;

AFAIK, not.

Vincent

Reply by email

Joao Morais
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 7 lines
On 10/12/07, Marc Santhoff 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;
Joao Morais

Reply by email

Joao Morais
to Vincent Snijders: It would be "published". AFAIK, not. Vincent
Vincent Snijders wrote 16 lines
Marc Santhoff schreef: > Hi, > > the reference manual is not explicitly clear in describing class > declarations. Is it true for classes as for objects that the first block > of components after the class header are visible as if declared public? > > E.g. > > TAnyClass = class > fField:anytype; > end; > > would make fField visible "public"? It would be "published".

iirc only if the class or a superclass is declared with $M+

Joao Morais

Reply by email

Mattias Gaertner
to Vincent Snijders: It would be "published". AFAIK, not. Vincent
Show quoted text 17 lines
Marc Santhoff schreef: > Hi, > > the reference manual is not explicitly clear in describing class > declarations. Is it true for classes as for objects that the first > block of components after the class header are visible as if > declared public? > > E.g. > > TAnyClass = class > fField:anytype; > end; > > would make fField visible "public"? It would be "published".

Classes with RTTI information use published, otherwise public. See the {$M+} switch, for example TPersistent.

Show quoted text 14 lines
> > And one other question: > 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? > > Smth. like: > > TAnyClass = abstract class(...) > ... > end; AFAIK, not.

No.
Just add abstract methods and you will get compiler warnings when instantiating such a class.

Mattias

Reply by email

Mattias Gaertner
to Joao Morais: Only if creating an instance straight from the class, eg: This will warn: begin VMyObj :=…
Show quoted text 23 lines
Felipe Monteiro de Carvalho wrote: > On 10/12/07, Marc Santhoff 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;

Mattias

Reply by email

Marc Santhoff
to Mattias Gaertner
Mattias Gaertner wrote 37 lines
On Fri, 12 Oct 2007 18:41:19 -0300 Joao Morais wrote: > Felipe Monteiro de Carvalho wrote: > > On 10/12/07, Marc Santhoff 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

Reply by email