I guess it behaves the way I would logically expect it to.  If I am
overriding a virtual function, then, darn it, it should be overridden for
the implementation of my class, whether called from the constructor or
elsewhere!

I concur with a previous respondent who mentioned that it's simply a bad
idea to call a potentially-overridden method from within your constructor,
if that method does something critical to the object construction.

-Rob

-----Original Message-----
From: Frans Bouma [mailto:[EMAIL PROTECTED]
Sent: Thursday, June 19, 2003 11:33 AM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Partially constructed objects in C#


it is indeed weird that C# would call from the base class the derived
method. That kind of behaviour you'd expect from calling abstract methods in
the base class.

FB

> 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
>
>

Reply via email to