Is there any reason why constructors in object pascal can not be private?
Asked
Active
Viewed 107 times
0
-
Constructors aren't private in C++, either: `struct Foo { Foo() = default; };` – Kerrek SB Apr 10 '16 at 21:24
-
1you do not understand, @KerrekSB, have you tried to do a singleton in delphi/pascal? In c++ you can change constructor's visibility to private but in delphi that's not possible. – Wellington Silva Ribeiro Apr 10 '16 at 21:27
-
Ah OK Maybe [this is relevant](https://stackoverflow.com/questions/5392107/delphi-singleton-pattern)? – Kerrek SB Apr 10 '16 at 21:30
-
@KerrekSB, look a singleton pattern in this link you'll see the visibility of constructor as private: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern – Wellington Silva Ribeiro Apr 10 '16 at 21:32
-
Why can't you declare your constructor private? – Sertac Akyuz Apr 10 '16 at 21:34
-
offcourse is relevant in delphi we should use tricks to have a singleton, because i can alwasys call the public constructor, thats goes against the standard used by many languages – Wellington Silva Ribeiro Apr 10 '16 at 21:37
-
See this answer: http://stackoverflow.com/a/14003208/243614 – Sertac Akyuz Apr 10 '16 at 21:39
-
To have a private constructor, you have to use a trick that declares a private constructor! ??? – Sertac Akyuz Apr 10 '16 at 22:28
-
@WellingtonSilvaRibeiro, not so, see the duplicate, more specifically Uwe's answer. – Johan Apr 11 '16 at 00:05
-
@SertacAkyuz, LOL missed that, thanks that's actually funny. :-) – Johan Apr 11 '16 at 00:38
-
1@KerrekSB: unlike C++, Delphi has a rooted class system, and constructors are inherited. So even hiding a constructor as private will still surface the root class's constructor, TObject.Create. That can not be properly hidden. – Rudy Velthuis Apr 11 '16 at 13:39