Not entirely. While the real calling convention of a constructor is indeed compiler magic the compiler knows about TObject's constructor (or more precisely the fact that TObject has a constructor) and uses that knowledge. In C++ on the other hand a class does not need to have a constructor thus the compiler needs to generate a default one. That's not the case in Object Pascal
.
Well, after reading all the post, i think that the issue is this: Free pascal chooses (by design) have a common root class, with (INMHO) has good pros, but several cons.
Good pros:
- Common destroy method (you can do freeandnil for every class)
- Common create method.
- No need for constructor and destructor declaration.
- Nice syntax for "compiler magic" like classname as being incorporated in tobject.
Cons:
- No way to override .create method effectively. You can have any class with a different constructor parameters (for example .create (neededparam1:integer) ) and a call to .create without parameters allways give a valid memory reference and this can be an error producer. -> Can .create be overrided easily for these cases? (i would love that a create (param1) could override a create without params... or things like that.
- No way to make a lighter class not dependant of TObject or without predefined methods, constructors or destructor.
- Not all are in .create method of Tobject. Some things like "class name" are initialiced by "compiler magic" (i like this term to call the compiler-black-box
)
Doubts:
- Inherited create could be a waste of time calling it to a TObject unless optimized? (is really optimiced to produce no code?). I only see that if inherited is used and no ancestor, it's removed (good, optimal), but in .create there is an ancestor.
And, well, my wishes....
I think (inmho) what is blowing me (
) is that all classes have their default constructor and destructor Even if already have one. I feel more confortably if a class has his default constructor if there isn't already a constructor. Or better, a default constructor.
And i know the way free pascal is doing it. Perhaps it could be extended by syntax. A "default" keyword could do the job (or some other keyword). For example, a class tree can only have one "default" constructor (declared as "default constructor") and if other "default constructor" is declared, then takes the job of the old. So you can declare all constructor you would like and only one "default" that it's checked.
Example of some enhanced syntax that could behave like before and after:
type TObject=class
public
default constructor create; {a default constructor if not one declared, an example of enhanced syntax -> not current in fpc}
default destructor destroy;
end;
type myclass=class (TObject);
public
default constructor create (param1:integer); {it hides the .create constructor, because other is declared}
constructor create (param1, param2:integer);
end;
Other options: that override (and/or reintroduce) could be used to override efectively other chosen method not necesary the same with same parameters.
This options has far more power because it could be used for all methods.
Example
type myclass=class (TObject);
public
constructor create (param1:integer); Override create; {extending override sintax to override exactly a chosen method; same would be for reintroduce}
{overriding one method with new one even if it's not having same or matching ones parameters. A tool to deprecate methods}
constructor create (param1, param2:integer);
end;
Note: My FPC knowledge is not too far. Perhaps the above examples have their own way of FPC of doing these, but i do not find these.
Note 2: Is there a forum to propose extended syntax to Devs or see the current syntax developing?. -> I'm really a newbie in the forum