Re: Rationale behind constructor call chain... ( and comparison with C++)
- From: "andrewmcdonagh" <andrewmcdonagh@xxxxxxxxx>
- Date: 30 Oct 2006 14:37:24 -0800
On Oct 30, 9:56 pm, "A.B." <a...@xxxxxxxxxxxxxxx> wrote:
Just a question...
If an object B extends an object A, whenever B is constructed, A is
constructed... if A() calls a
function that is overloaded in B, it looks like the overloaded function
is called by the A constructor ...
( In C++ the behavior is always to call the function defined in A when
called within the A constructor, even if the overloaded function is B
is virtual. )
Can someone explain me the rationale behind java's behavior in that
case, how it's useful, more logical etc than the C++ approach... can
this behavior be overriden?
Thanks!
The C++ approach is correct for C++.
The Java(and C#) approach is correct for Java(and C#).
The languages deal with calls to virtual methods during constructor
calls in ways that make sense for those languages.
Its not a question over which is a better way.
In any case, its commonly understood to be 'a bad thing' in all 3
languages.
The 'Effective C++ book warns against it', see Chapter 2, Item 9: Never
call virtual functions during construction or destruction. page 48.
The same is true for Java and C# (but I can't google a good source for
you to see).
On the other hand, here's a Java RFI that was rejected....with
reasons...
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4378291
Regards
Andrew
.
- References:
- Prev by Date: Re: Rationale behind constructor call chain... ( and comparison with C++)
- Next by Date: Re: How to set a character of a String?
- Previous by thread: Re: Rationale behind constructor call chain... ( and comparison with C++)
- Next by thread: Re: Rationale behind constructor call chain... ( and comparison with C++)
- Index(es):
Relevant Pages
|