Overloaded constructors - safer, easier structure?

From: Martin James (mjames_falcon_at_dial.pipex.com)
Date: 11/29/03


Date: Sat, 29 Nov 2003 15:02:53 -0000

I've got these overloaded constructors:

**********************************
constructor TmyClass.create(someParams);
begin
    beforeStuffDependsOnSomeParams;
    otherBeforeStuff;
    inherited create(params);
    afterStuff;
end;

constructor TmyClass.create(otherParams);
begin
    beforeStuffDependsOnOtherParams;
    otherBeforeStuff;
    inherited create(params);
    afterStuff;
end;
**********************************

Can I somehow put all the common code into a procedure/function?

**********************************
function TmyClass.initialize:TmyClass;
begin
    otherBeforeStuff;
    result:=inherited create(params);
    afterStuff;
end;

constructor TmyClass.create(someParams);
begin
    beforeStuffDependsOnSomeParams;
    self:=initialise;
end;

constructor TmyClass.create(otherParams);
begin
    beforeStuffDependsOnOtherParams;
    self:=initialise;
end;
**********************************

OR in some other way put all my common code in one place so that when I
create more objects that always need to be there I don't forget to include
the code in one of the constructors?

Rgds,
Martin



Relevant Pages