I have a TObject reference to a instance of an unkown class. How do I call the constructor of this unknown class to create another instance of it? I know that Delphi has RTTI, but it isn't clear how to use it.
You can't construct an object of unknown type. The compiler has to know the correct class type at compile-time so it can generate the proper code. What if the constructor requires parameters? How many? What data types? Are they passed by stack or registers? This information is important.
That being said, if the classes in question are all derived from a common base class that has a virtual constructor, THEN and ONLY THEN can you construct such objects. You can use the TObject.ClassType()
method to get a reference to the class type of the existing object, type-cast it to the base class type, and call the constructor. For example:
type
TBase = class
public
constructor Create(params); virtual;
end;
TBaseClass = class of TBase;
TDerived1 = class(TBase)
public
constructor Create(params); override;
end;
TDerived2 = class(TBase)
public
constructor Create(params); override;
end;
...
var
NewObj: TBase;
begin
if SomeObj is TBase then
NewObj := TBaseClass(SomeObj.ClassType).Create(params);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With