Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do TObject.AfterConstruction and BeforeConstruction have public visibility?

Today, one very simple question came to my mind when I had to override TObject's BeforeConstruction method:

Why do TObject methods AfterConstruction and BeforeConstruction have public visibility?

I expected them to be protected. What is the reason they aren't?

I can't imagine a valid purpose to call AfterConstruction or BeforeConstruction without calling the constructor or destructor of that class. Do you?

like image 906
René Hoffmann Avatar asked May 08 '15 10:05

René Hoffmann


1 Answers

A previous question asked why some other methods are public instead of protected, and the answer was that they at some point needed to be called by utility functions that weren't attached to the class.

TObject.AfterConstruction is called by just such a utility function, System._AfterConstruction. If it were (strict) protected, then that standalone function wouldn't have access to the method.

All the methods of TObject are public.* We can probably find rationales to explain why each method is public, but at a certain point, I suspect that the underlying reason for any given method being public is that all the others are, too.

Once Delphi was released with those methods public, any reduction in their visibility would have risked breaking existing code.


* Except for GetDisposed and CheckDisposed, for some reason. They're relatively new, compared to the bulk of TObject.

like image 99
Rob Kennedy Avatar answered Oct 15 '22 19:10

Rob Kennedy