Essentially, boiled down, the debate ran something like this: Stroustrup commented, in one of his many tirades against java, that he truly disliked the behavior Java offers, not because of any efficiency concern, but that it allowed precisely the hole that you guys have just discovered--a virtually-dispatched method call may invoke methods or reference fields on part of an object that's not yet been initialized. He felt this was a huge violation of consistency, and so wrote C++ to incrementally initialize its vtable (and virtual method dispatch displacements) accordingly.
Gosling, on the other hand, felt that C++'s approach to virtual method dispatch was inconsistent--that at any given point in the object's lifetime, the dispatch resolution for a virtual method should always be the same, regardless of whether you're in a constructor or not. Again, not really much to do with efficiency as it was with language purity. (Remember, the compiler could, within a constructor, emit an "invokespecial"--the JVM equivalent to a "call"--instead of a "invokevirtual"--the JVM equivalent to "calli"--for any method call on this class while called from within a constructor body. "Invokespecial" (and its CLI equivalents) can effectively bypass virtual method dispatch, so it's not impossible to do if they wanted to.) It's really just a question of which semantics you prefer. Stroustrup felt one way, Gosling (and Haljsberg) felt differently. Don't like it, write your own C#. :-) Ted Neward Author, Instructor, Presenter: Java and .NET http://www.neward.net/ted/weblog http://www.javageeks.com http://www.clrgeeks.com > -----Original Message----- > From: Moderated discussion of advanced .NET topics. [mailto:ADVANCED- > [EMAIL PROTECTED] On Behalf Of Williams, Hugh > Sent: Thursday, June 19, 2003 9:12 AM > To: [EMAIL PROTECTED] > Subject: Re: [ADVANCED-DOTNET] Partially constructed objects in C# > > Interesting -- I tried it in Java too; it behaves the same as C#. Perhaps > the Java guys have some perspective to offer? > > -----Original Message----- > From: Stephen Dunn [mailto:[EMAIL PROTECTED] > Sent: Thursday, June 19, 2003 9:59 AM > To: [EMAIL PROTECTED] > Subject: [ADVANCED-DOTNET] Partially constructed objects in C# > > > Thanks to FxCop, I've just discovered quite a fundamental difference > between > C++ and C#. > > Check out the following code. > > class Base > { > public Base( ) > { > Method( ) ; > } > > public virtual void Method( ) > { > Console.Write("I'm the Base"); > } > } > > class Derived > : Base > { > public Derived( ) > { > Method( ) ; > } > > public override void Method() > { > Console.WriteLine("I'm the Derived"); > } > } > > Constructing a Derived object means a call is made to Base's constructor > which calls Method; not Base's method (as in C++), but Derived's Method. > At > this point, Derive is only partially constructed. When I stumbled over > this > in C++, I was surprised that the base constructor didn't call the derived > method. I soon changed my mind when it was explained to me that C++ is > implemented this way as unexpected results could happen by called > Derived's > virtual method. > > Would anyone like to comment? Personally, I feel C++ is correct in this > instance. > > Cheers, > > Steve