Re: Rationale behind constructor call chain... ( and comparison with C++)





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

.



Relevant Pages

  • Re: OOP php user system
    ... those singletons the constructor args. ... design, even in Python. ... rather to always have getter and setter methods. ... That's why you won't find them in good OO languages such as SmallTalk, ...
    (comp.lang.php)
  • Re: Factory Design Pattern and Select Case
    ... Maybe you're assuming a switch is done by an integer code which rarely ... Some languages even have literal ... Calling the constructor is code, knowing what constructor to call is ... literals in most languages, and even for hashtables in fewer languages), ...
    (comp.object)
  • Re: initializing mutable class attributes
    ... but from my experience with languages which consistently try to ... just impossible to use the principle "implicit is better than explicit" ... >> constructor for that class. ...
    (comp.lang.python)
  • Pattern matching (was: Parallel Common-Lisp with at least 64 processors?)
    ... dispatch over nested algebraic data types) as seen in F#, OCaml, ... knows that particular part of ML or F# or OCaml or Haskell or SML ... references to the way it's done in various other languages. ... the constructor is not ...
    (comp.lang.lisp)
  • Re: Design Patterns and Functional programming
    ... Impure functional programming languages like Mathematica, Lisp, Scheme, ... a pattern match gives yes/no ... What is the subject of matching. ... body of another function, here a constructor. ...
    (comp.object)