Board index » delphi » Constructor question

Constructor question

Hi folks

I have an object:

TObj1 = class (TObject)
....
constructor Create( x: integer ); virtual;
...

and another object inheriting from this:

TObj2 = class( TObj1)

TObj 2 has a slightly different constructor:

constructor Create( x,y: integer );

To get round this I have been using

constructor Create( x,y: integer ); reintroduce;

then calling inherited Create (x) in the constructor.

Is this ok?

Ta

Will

 

Re:Constructor question


Quote
William Buchanan wrote:
> I have an object:

> TObj1 = class (TObject)
> ....
> constructor Create( x: integer ); virtual;
> ...

> and another object inheriting from this:

> TObj2 = class( TObj1)

> TObj 2 has a slightly different constructor:

> constructor Create( x,y: integer );

> To get round this I have been using

> constructor Create( x,y: integer ); reintroduce;

> then calling inherited Create (x) in the constructor.

> Is this ok?

You have not presented a case as to why that directive (reintroduce)
would be necessary.

--
David Farrell-Garcia
Orca Software & Technologies

Posted With XanaNews  1.12.2.2

Re:Constructor question


Quote
> Is this ok?

Don't make the first constructor virtual if you're not going to override
it. You can also have a look at using the overload directive if you want

Quote
> 1 constructor. In the example given, I wouldn;t have thought reintroduce was strictly necessary.

Cheers,
Jim Cooper

____________________________________________

Jim Cooper      jcoo...@tabdee.ltd.uk
Tabdee Ltd      http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi with your Palm
____________________________________________

Re:Constructor question


Quote
> You have not presented a case as to why that directive (reintroduce)
> would be necessary.

Basically to suppress the warning that the create method is being hidden....
What I really want to know is am I doing this the correct way. I just
supplied  the code as an example.

Thanks for the reply

Will

Quote

> --
> David Farrell-Garcia
> Orca Software & Technologies

> Posted With XanaNews  1.12.2.2

Re:Constructor question


Quote
Dennis Landi wrote:
> It is better design.  He cannot entirely predict what uses his class
> heirarchy
> will have in the future, especially if this is to be deployed in a
> real enterprise that must be allowed to organically grow/change over
> time.  There is no need to deny himself the flexibility of heirarchy
> design the "virtual" directive allows him.

it is seldom good design to do that.  In most cases this would be an
example of bad planning and he would have been better off using an
interface(s)  or even an abstract class.

--
David Farrell-Garcia
Orca Software & Technologies

Posted With XanaNews  1.12.2.2

Re:Constructor question


Quote
> > Basically to suppress the warning that the create method is being
hidden....
> > What I really want to know is am I doing this the correct way.

Nothing wrong with that.

Quote
> The real question is (and Jim Cooper already mentioned it): why is the
> constructor virtual in the first place? There is absolutely no need to
> make constructors virtual if you are going to hide them anyway.

It is better design.  He cannot entirely predict what uses his class
heirarchy
will have in the future, especially if this is to be deployed in a real
enterprise that must be allowed to organically grow/change over time.  There
is no need to deny himself the flexibility of heirarchy design the "virtual"
directive allows him.

Obj1-
         |
          Obj2

Is only one heirarchy branch.

There may well be a

Obj1-
        |
        Obj3
            |
            Obj4

With entirely different method signatures, all of which a valid
descendants....  But the point is he should be allowed to do this 12 months
from now, and it should be allowed to flow from his current design.

-dennis

Re:Constructor question


Quote
William Buchanan wrote:
> Basically to suppress the warning that the create method is being
> hidden....  What I really want to know is am I doing this the correct
> way. I just supplied  the code as an example.

Then why  did you make the base class constructor virtual in the first
place?  Is what you really want an abstract base class?  If so make it
so, or use and interface.

--
David Farrell-Garcia
Orca Software & Technologies

Posted With XanaNews  1.12.2.2

Re:Constructor question


Quote
Dennis Landi wrote:
> And so your plans have
> greater integrity/longevity.  

We can only hope :-)

--
David Farrell-Garcia
Orca Software & Technologies

Posted With XanaNews  1.12.2.2

Re:Constructor question


ha ha !

Quote
"David Farrell-Garcia" <dav...@orcasoftware.com> wrote in message

news:3dd939de$1@newsgroups.borland.com...
Quote
> Dennis Landi wrote:

> > And so your plans have
> > greater integrity/longevity.

> We can only hope :-)

> --
> David Farrell-Garcia
> Orca Software & Technologies

> Posted With XanaNews  1.12.2.2

Re:Constructor question


Quote

> it is seldom good design to do that.  In most cases this would be an
> example of bad planning and he would have been better off using an
> interface(s)  or even an abstract class.

Like you can see in some VCL components when you try to develop
descendants from them.

Re:Constructor question


In article <3dd91...@newsgroups.borland.com>, William Buchanan says...

Quote
> > You have not presented a case as to why that directive (reintroduce)
> > would be necessary.

> Basically to suppress the warning that the create method is being hidden....
> What I really want to know is am I doing this the correct way. I just
> supplied  the code as an example.

The real question is (and Jim Cooper already mentioned it): why is the
constructor virtual in the first place? There is absolutely no need to
make constructors virtual if you are going to hide them anyway. And you
can easily hide them if they are not virtual. That is actually the norm.
--
Rudy Velthuis (TeamB)

"We should leave our minds open, but not so open that our brains
 fall out." - Alan Ross Anderson

Re:Constructor question


Quote
> <snip>  Is what you really want an abstract base class?

Only if the root class' method is empty, and only if he is using the top
level class ancestor as a design template ala tStream.  It would appear the
the top level class in this case executes code that all descendants would
use.

Having said all that, William, I don't do this method overloading on the
constructor (yes, I know there is only one method in the example...) very
often.  I prefer to create an "Initialize" method that I call after the
constructor.  But sometimes you have to mess with the constructor if it is
imperative that you pass in run-time parameters that affect how the class
instance is created.  When I find myself needing to do this a lot, for each
class descendant in a class heirarchy, I question my overall design.  Is
there a better way of designing the classes?  Maybe there is a helper class
hidden in there some place that you need to identify and expose...

Hope that helped.

-dennis

Quote
"David Farrell-Garcia" <dav...@orcasoftware.com> wrote in message

news:3dd92b95$1@newsgroups.borland.com...
Quote
> William Buchanan wrote:

> > Basically to suppress the warning that the create method is being
> > hidden....  What I really want to know is am I doing this the correct
> > way. I just supplied  the code as an example.

> Then why  did you make the base class constructor virtual in the first
> place?  Is what you really want an abstract base class?  If so make it
> so, or use and interface.

> --
> David Farrell-Garcia
> Orca Software & Technologies

> Posted With XanaNews  1.12.2.2

Re:Constructor question


Speaking of virtual constructors, why do they exist anyway?
Why need a contructor be virtual, because it is always
called through a class name, not an object name.There is
no polymorphism involved.

Re:Constructor question


Quote
Hamilton Inc wrote:
> Speaking of virtual constructors, why do they exist anyway?
> Why need a contructor be virtual, because it is always
> called through a class name, not an object name.There is
> no polymorphism involved.

they are also called via class references, that's where virtual
constructors needed, and where polymorphism involved

Re:Constructor question


Quote
> Why need a contructor be virtual

Besides the case where you have a class reference rather than a class
name, there is also this sort of situation (assuming other properties
and methods I have left out of the definitions) :

TClass1 = class(TMyObject)
public
  constructor Create; virtual;
end;

TClass2 = class(TClass1)
public
  constructor Create; override;
end;

TClass3 = class(TClass1)
end;

The most common reason for this is that TClass2 has some extra field
that needs to be instantiated in the overridden constructor, but TClass3
doesn't.

Cheers,
Jim Cooper

____________________________________________

Jim Cooper      jcoo...@tabdee.ltd.uk
Tabdee Ltd      http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi with your Palm
____________________________________________

Go to page: [1] [2] [3]

Other Threads