Why do TObject.AfterConstruction and BeforeConstruction have public visibility?
Asked Answered
S

1

11

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?

Sent answered 8/5, 2015 at 10:10 Comment(8)
I'm sure this was asked (or maybe not asked, but answered)...Plummer
Likely the developer made a mistakeHinds
Oh, so it's been about TObject.InitInstance, and does not directly answer what you ask. But as you can see, there's more that should've been protected but it's not. Taking back my initial comment...Plummer
Maybe, @DavidHeffernan is right with his assumption. Dr. Bob wrote this on Delphi 4. It says that these methods have been protected back then.Moravian
If they were protected but have been made public on a later date, it was very likely not a mistake, bur for a certain purpose.Had
Having the methods to be public allows you to intercept them using TVirtualMethodInterceptorCharissacharisse
@Charissacharisse Virtual methods can be intercepted irrespective of their visibility.Hinds
I had a similar question once: #22541170Porosity
P
10

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.

Promotion answered 8/5, 2015 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.