| View previous topic :: View next topic |
| Author |
Message |
Martin James Guest
|
Posted: Wed Dec 15, 2004 10:00 am Post subject: Abstract constructor? |
|
|
If I define a completely abstract class from TObject, eg:
Tmailbox=class(Tobject)
private
function getCount: integer; virtual; abstract;
protected
public
property count:integer read getCount;
constructor create; virtual; abstract;
procedure push(aObject:Tobject); virtual; abstract;
function pop(pResObject:pObject;timeout:DWORD):boolean; virtual;
abstract;
destructor destroy; virtual; abstract;
end;
I cannot call any inherited create in my descendant classes. I have to have
a constructor because the TObject constructor is static. I know that the
TObject constructor does not do anything, but even so it looks wrong when I
do not call any inherited create in the children.
I can easily get round it by taking the'abstract' off the constructor and
actually using a virtual constructor that calls 'inherited'. That looks
right, but it seems like I am calling two constructors that do nothing :(
Is there any other recommended design/bodge?
Rgds,
Martin
PS. I was tempted to change my sig to 'Castro', but managed to resist :)
|
|
| Back to top |
|
 |
Chris Trueman Guest
|
Posted: Wed Dec 15, 2004 10:06 am Post subject: Re: Abstract constructor? |
|
|
Maybe I'm missing something but why not define an interface with the
Tmailbox methods and inherited from that?
Chris.
|
|
| Back to top |
|
 |
Joanna Carter (TeamB) Guest
|
Posted: Wed Dec 15, 2004 10:14 am Post subject: Re: Abstract constructor? |
|
|
"Chris Trueman" <ctrueman (AT) wavesoftware (DOT) com> a écrit dans le message de news:
Xns95C0671CC4F2Dctruemanwavesoftware (AT) 207 (DOT) 105.83.66...
| Quote: | Maybe I'm missing something but why not define an interface with the
Tmailbox methods and inherited from that?
|
Possibly because the OP doesn't understand how to manage the memory problems
mixing objects and interfaces causes ? :-)
Joanna
--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Wed Dec 15, 2004 10:19 am Post subject: Re: Abstract constructor? |
|
|
| Quote: | I cannot call any inherited create in my descendant classes.
|
Well, no, you can't call TMailBox.Create because it's abstract Why
not just leave it out altogether?
| Quote: | I have to have a constructor because the TObject constructor is static.
|
Can you explain that reasoning a bit?
Cheers,
Jim Cooper
_______________________________________________
Jim Cooper [email]jim (AT) falafelsoft (DOT) com[/email]
Falafel Software http://www.falafelsoft.com
_______________________________________________
|
|
| Back to top |
|
 |
Martin James Guest
|
Posted: Wed Dec 15, 2004 10:46 am Post subject: Re: Abstract constructor? |
|
|
| Quote: |
I cannot call any inherited create in my descendant classes.
Well, no, you can't call TMailBox.Create because it's abstract Why
not just leave it out altogether?
I have to have a constructor because the TObject constructor is static.
Can you explain that reasoning a bit?
|
I cannot override a static method.
Rgds,
Martin
|
|
| Back to top |
|
 |
Stefan Meisner Guest
|
Posted: Wed Dec 15, 2004 11:02 am Post subject: Re: Abstract constructor? |
|
|
| Quote: | I cannot override a static method.
|
But you could declare the constructor as being virtual in your descending
class, like TPersistent does.
Greetings
Stefan
|
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Wed Dec 15, 2004 11:20 am Post subject: Re: Abstract constructor? |
|
|
| Quote: | I cannot override a static method.
|
No, but I still don't see your problem. Why do you see a need for a
constructor in your TMailbox class? Just introduce a constructor when
you need to. Are you worried about polymorphic construction of objects?
Cheers,
Jim Cooper
_______________________________________________
Jim Cooper [email]jim (AT) falafelsoft (DOT) com[/email]
Falafel Software http://www.falafelsoft.com
_______________________________________________
|
|
| Back to top |
|
 |
Bob Dawson Guest
|
Posted: Wed Dec 15, 2004 12:34 pm Post subject: Re: Abstract constructor? |
|
|
"Jim Cooper" wrote
| Quote: | need to. Are you worried about polymorphic construction of objects?
|
I assumed that as the intent, yes, so the code
obj := TmailboxClass.Create;
would function properly.
Personally I'm not at all bothered by a virtual constructor that does
nothing but call inherited, and I wouldn't optimize away from that standard
code without very strong reason. Optimization at the program logic, domain
model, and algorithm levels is generally much more important than optimizing
individual code lines.
bobD
|
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Wed Dec 15, 2004 3:54 pm Post subject: Re: Abstract constructor? |
|
|
| Quote: | I assumed that as the intent, yes, so the code
obj := TmailboxClass.Create;
would function properly.
|
But that'll function without a constructor defined in TMailBox at all :-)
I don't have an issue with a one line virtual constructor either, but
I'm not sure what the issue actually is
Cheers,
Jim Cooper
_______________________________________________
Jim Cooper [email]jim (AT) falafelsoft (DOT) com[/email]
Falafel Software http://www.falafelsoft.com
_______________________________________________
|
|
| Back to top |
|
 |
Wayne Niddery [TeamB] Guest
|
Posted: Wed Dec 15, 2004 4:10 pm Post subject: Re: Abstract constructor? |
|
|
Martin James wrote:
| Quote: |
Tmailbox=class(Tobject)
public
constructor create; virtual; abstract;
destructor destroy; virtual; abstract;
end;
I cannot call any inherited create in my descendant classes. I have
to have a constructor because the TObject constructor is static. I
know that the TObject constructor does not do anything, but even so
it looks wrong when I do not call any inherited create in the
children.
I can easily get round it by taking the'abstract' off the constructor
and actually using a virtual constructor that calls 'inherited'.
|
That's the correct route.
| Quote: | That looks right, but it seems like I am calling two constructors
that do nothing
|
Let the compiler/optimizer worry about that.
A worse problem is your virtual spec on the destructor. Destroy is *already*
virtual. You are destroying TObject polymorphism by redclaring this as
virtual here. You should remove this altogether unless you need it to
actually do something, and in that case, specify override, not virtual.
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
SpaceShipOne; GovernmentZero
|
|
| Back to top |
|
 |
Bob Dawson Guest
|
Posted: Wed Dec 15, 2004 4:56 pm Post subject: Re: Abstract constructor? |
|
|
"Jim Cooper" wrote
| Quote: |
I assumed that as the intent, yes, so the code
obj := TmailboxClass.Create;
would function properly.
But that'll function without a constructor defined in TMailBox at all
|
Not if you're looking for a descendent object and TMailBox doesn't redefine
the constructor as virtual.
bobD
|
|
| Back to top |
|
 |
Rob Kennedy Guest
|
Posted: Wed Dec 15, 2004 5:41 pm Post subject: Re: Abstract constructor? |
|
|
Bob Dawson wrote:
| Quote: | "Jim Cooper" wrote
I assumed that as the intent, yes, so the code
obj := TmailboxClass.Create;
would function properly.
But that'll function without a constructor defined in TMailBox at all :-)
Not if you're looking for a descendent object and TMailBox doesn't redefine
the constructor as virtual.
|
The constructor doesn't need to be virtual. Martin could use the
following code, for instance.
type
TMailbox = class
public
procedure DoCreate; virtual; abstract;
constructor Create; // not virtual
end;
TMailboxClass = class of TMailbox;
constructor TMailbox.Create;
begin
inherited Create;
DoCreate;
end;
He might not even need to use DoCreate at all. Maybe he could just
override the AfterCreation method instead. And then TMailbox couldn't
need a separate constructor, either.
(If you're descending from TInterfacedObject, or you use a similar
implementation for some other interfaced class, be careful about the
order in which you call the inherited AfterConstruction method. That's
the method that sets the object's reference count back to zero, so you
mustn't allow any references to the object to be released after you call
the inherited method.)
--
Rob
|
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Wed Dec 15, 2004 5:43 pm Post subject: Re: Abstract constructor? |
|
|
| Quote: | Not if you're looking for a descendent object and TMailBox doesn't redefine
the constructor as virtual.
|
Well, it depends. That's why I asked for clarification of the problem.
I've using the Template Method pattern as per Rob's post before. Other
times I've had a virtual constructor. Hard to give advice without
knowing what the problem is :-)
Cheers,
Jim Cooper
_______________________________________________
Jim Cooper [email]jim (AT) falafelsoft (DOT) com[/email]
Falafel Software http://www.falafelsoft.com
_______________________________________________
|
|
| Back to top |
|
 |
Cesar Romero Guest
|
Posted: Sat Dec 18, 2004 2:59 pm Post subject: Re: Abstract constructor? |
|
|
| Quote: |
I cannot override a static method.
|
I usually do reintroduce when I need a diferent constructor
TBox = class(TObject)
public
constructor create(Mechanism: TCustomMechanism;
Father: TObject); reintroduce; override;
....
end;
|
|
| Back to top |
|
 |
|