On 04.10.2013 14:48, Dennis Poon wrote:
>You can use either FillChar as you did or use "Self := Default(TTest);" (default is a compiler intrinsics that returns a 0 value of the type >you passed in, e.g. Nil for classes, '' for strings, 0 for ordinals and for records all fields are set to 0) How can I make it so that I only do the Self := Default() once in the parent class constructor and don't have to repeat it in all its descendant class constructor.
Why would you want to do that? Classes are already zero initialized when the constructor is entered. Also using "Self := Default(...)" inside a class would not do what you expect it to do. "Self" is a class reference and thus "Default(...)" of the same type would return "Nil" and thus set your "Self" for the current method call to "Nil" (so you could no longer access the class instance at all).
I noticed in your example, you have to specify the class name TTest but that implies in every descendant class, I have to replace it with the Descendant class name. Can I do self := Default(ClassType) ? hoping it will return the descendant's class type?
Default expects a type identifier and nothing else. I currently don't know whether Delphi supports more. But in any way it wouldn't change much as Default(TObject) = Default(TSomeClass) = Default(TSomeChildClass) = Nil.
Regards, Sven _______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal