I agree with Stefan in that it's not an issue of right or wrong. I suppose it boils down to the fundamental decision/opinion as to whether an object is an object, despite not having all of its constituent parts. If you were to follow a sheet of metal along a car production line, at what point would you call it "car"? Would it be just from the intent of turning a sheet of metal into a car? Or would it be when the very last bolt was added (and tightened to the correct torque). If the latter, it is worth asking yourself if a part drops off the "car", does it mean it's no longer a car? Hmm,
Steve. -----Original Message----- From: Stefan Holdermans [mailto:[EMAIL PROTECTED] Sent: 19 June 2003 16:26 To: 'Stephen Dunn'; 'Moderated discussion of advanced .NET topics.' Stephen, Well, I don't think it's an issue of being correct here. The downside of the way it is implemented in .NET is that you can call a method of a partially constructed object---just as you have pointed out. The negative thing about the C++ approach is that you wind up with different semantics for calling virtual method in constructor and non-constructor code. Regards, Stefan >-----Original Message----- >From: [EMAIL PROTECTED] >[mailto:[EMAIL PROTECTED] On Behalf Of Stephen >Dunn >Sent: Thursday, June 19, 2003 3:59 PM >To: 'Moderated discussion of advanced .NET topics.' >Subject: 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 >