"cody" <
[email protected]> a écrit dans le message de eNWx5%
[email protected]...
| Yes I head that ctors in delphi can actually return null
Only if an exception occurs in the constructor.
| Does delphi suffer from that problem? Would the OP's program work with
| delphi?
Peter is an old hand at Delphi, just like me; his program would use the the
fact that all constructors in base classes are visible throughout the
hierarchy, whether they be virtual or not.
This visibility can, IMO, cause more problems than it cures. TObject is the
base class of all reference types, sort of analagous to System.Object but
without the reflection metadata but also with a ClassType() method that
returns an instance of TClass, a sort of metaclass, sort of analagous to
System.Type but with a Create() method which is effectively a constructor
for TObject.
This arrangement allows you to do some cool things without having to create
class factories :
TFruit = class
...
public
constructor Create; virtual; abstract
end;
TApple = class(TFruit)
...
public
constructor Create; override;
end;
TOrange = class(TFruit)
...
public
constructor Create; override;
end;
TFruitClass = class of TFruit;
var
fruitCreator: TFruitClass;
fruit: TFruit;
begin
fruitCreator := TApple;
fruit := fruitCreator.Create; //creates an Apple
fruitCreator := TOrange;
fruit := fruitCreator.Create; // creates an Orange
...
end;
Any pattern of constructor, parameterised or not, can be declared in the
base class as virtual and/or abstract and overridden in derived classes.
It is quite easy and feasible to create your own metaclasses in C# and have
virtual/abstract methods called Create(...) which then give the same idea,
essentially acting as class factories rather than the true metaclass found
in Delphi.
One major disadvantage of Delphi for Win32 is that the TObject Create
default constructor is public and not hideable, therefore you can always
circumvent a protected or private constructor such as you might use in a
Singleton class to ensure only one instance.
Joanna