Board index » delphi » Question about Class Members

Question about Class Members

Q1:
A class member function can be constructed by using 'class':

type
Ta = class(TObject)
   public
    class procedure Pa;
  end;

How can I add a class variable to class Ta?

Q2:
How can I add a member function to a class without a implementation,
forcing it's children to implement that function?

type
Ta = class(TObject)
  public
    // I want to force each child to implement this function
    procedure Pa;
    // I want to implement this function in the baseclass
    procedure Pb;
  end;

Tb = class(Ta)
  public
    procedure Pa;
  end;

Thanx&
CU
WS

 

Re:Question about Class Members


Quote
Wim Smit wrote:

> Q1:
> A class member function can be constructed by using 'class':

> type
> Ta = class(TObject)
>    public
>     class procedure Pa;
>   end;

> How can I add a class variable to class Ta?

You can't directly. Declare a 'normal' variable inside the implementation
section of the unit, and give the class some methods to access this:

 type
 Ta = class(TObject)
    public
     class procedure Pa;
     class function GetX: Integer;
     class procedure SetX(X: Integer);
   end;

 ...
 implementation

 var
   Ta_X: Integer= 0;

 ...
 class function Ta.GetX: Integer;
 begin
   Result := Ta_X;
 end;
 ...

Quote

> Q2:
> How can I add a member function to a class without a implementation,
> forcing it's children to implement that function?

> type
> Ta = class(TObject)
>   public
>     // I want to force each child to implement this function
>     procedure Pa;
>     // I want to implement this function in the baseclass
>     procedure Pb;
>   end;

> Tb = class(Ta)
>   public
>     procedure Pa;
>   end;

 Ta = class(TObject)
   public
     procedure Pa; virtual; abstract;
     procedure Pb;
   end;

 Tb = class(Ta)
   public
     procedure Pa; override;
   end;

HTH
-Michael

Re:Question about Class Members


Good morning...

Quote
Wim Smit <w.s...@magnacarta.nl> wrote in message

news:7tigtm$2qh$1@news1.xs4all.nl...

Quote
> Q1:
> A class member function can be constructed by using 'class':

> type
> Ta = class(TObject)
>    public
>     class procedure Pa;
>   end;

> How can I add a class variable to class Ta?

You can't, but what you can do is declare a variable inside the
implementation section of the unit that contains Ta, and class procedure Pa
will have access to it.

Quote
> Q2:
> How can I add a member function to a class without a implementation,
> forcing it's children to implement that function?

> type
> Ta = class(TObject)
>   public
>     // I want to force each child to implement this function
>     procedure Pa;
>     // I want to implement this function in the baseclass
>     procedure Pb;
>   end;

> Tb = class(Ta)
>   public
>     procedure Pa;
>   end;

type
Ta = class(TObject)
    public
    procedure Pa; virtual; abstract;
    procedure Pb;
end;

Tb = class(Ta)
    public
        procedure Pa; override;
end;

The virtual keyword tells the compiler to use late binding to find the
correct method to run at run time based of the type of object you've
substantiated.  abstract tells the compiler that you aren't implementing the
method in this class.  However, this does not FORCE Tb to implement Pa, but
if you compile a program that creates a Tb without implementing Pa, then you
will get a warning 'Warning: Creating instance of Pa using abstract
methods'.

Hope this helps,

Shane

Re:Question about Class Members


Quote
>> How can I add a class variable to class Ta?

>You can't directly. Declare a 'normal' variable inside the implementation
>section of the unit, and give the class some methods to access this:

Thanx, we are doing just that.
CU
WS

Other Threads