convert "class of" in pascal to D.

dennis luehring dl.soluz at gmx.net
Wed Jan 30 09:18:09 PST 2013


Am 30.01.2013 17:16, schrieb Eko Wahyudin:
> thanks all,
>
> I think i am understand, if the closest solution is template,
> there is no solution in D for my code like this.
>
> type
>         TMyObjectClass = class of TMyObjectA;
>
> var
>         ClassArray : array of TMyObjectClass; //the content
> initialized
> randomly
>         ObjectArray : array of TMyObjectA;
>         i, j : Integer;
>
> procedure CreateAllObject;
> begin
>         J:= length(ClassArray);
>         for I:= 0 to J do begin
>           ObjectArray[I]:= ClassArray[I].Create({some argument});
>         end;
> end;
>
> if D unable to do this, I think we must do something with D.
>
> I'm still thinking, why constructor in D adopt C++ style rather
> than pascal style, while pascal style is better :-?

sorry but the pascal "class of"-type and the ctor-derivation isn't that 
super-mighty at all
as long as the ctors of your object are the same everything is fine
else -> the feature isn't useable anymore, then you need a real object
factory

in real OOP objects are specialized through its virtual method 
implementations AND its ctor-parameters (which are similar in very very 
few rare cases)

for example pseudo code

class Stream
   virtual read_bytes()...

   class FileStream: Stream
     this(filename)

   class NetworkStream: Stream
     this(tcp_ip,timeout)

Stream[] streams

streams ~= FileStream("c:/temp/test.txt");
streams ~= NetworkStream("123.112.2.1", 1000);

stream[n].read_bytes()

this is a much more common OOP/ctor situation then yours
i think its part of pascal/object delphi to ease the VCL development

but this can be easily reached with an internal CreateInstance routine like

Stream
   virtual Stream CreateInstance()

and
   FileStream implements CreateInstance() with with return new FileStream
   NetworkStream "" with new NetworkStream etc.

so whats the realy big deal/feature of this "class of"-type except for
very trivial OOP case











More information about the Digitalmars-d mailing list