Flávio Etrusco wrote:
> Graeme, I guess the OP didn't want to reintroduce the 'Create'
> constructor with lower visibility, just implement a second constructor
> with private visiblity.
> It can be useful when implementing singletons and factories to avoid
> some types of misuse by an unattentive coworker...

The problem is that a constructor (in this case Create) in still always
public. So that unattentive coworker can very easily use the default
"public" Create() constructor and by-pass all your custom code in your
"private" constructor.

So that pretty much makes your custom private constructor useless. Plus
if the class was instantiated with the default public Create(), there
could be many other side-effects due to the fact that it by-passed your
custom code.

And yes I know, such a feature would be very handy for Singleton
implementations, but alternative solutions are possible. The same could
be applied to the original poster's code, without creating private or
protected constructors.

A very quick and dirty solution would be to reimplement the public
Create() constructor and immediately raise an exception inside it,
explaining the problem. That way if any developer tries to use that
default constructor, they will get an error - instead of some silent
side-effect later on. Then implement a new public custom constructor
with your desired code.


  TMyClass = class(TObject)
    constructor Create;
    constructor CreateCustom(param1: string);


  constructor TMyClass.Create;
    raise Exception.Create('Please use CreateCustom instead');

  constructor TMyClass.CreateCustom(param1: string);
    inherited Create; // <- note inherited call to bypass Exception
    FValue := param1;

No private or protected constructors are used and you are guiding the
developer to only use the CreatCustom constructor, ensuring your custom
code is executed.

This is still not a perfect solution, but minimized the potential problems.

> IIRC the last time this issue was raised you were on the side for
> removing the warning - my opinion too ;-)

Maybe I learnt from my mistakes. ;-)

  - Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal

fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org

Reply via email to