L
Laura T
Maybe this gives some hints:
(from http://blogs.msdn.com/scottwil/archive/2005/01/14/353177.aspx)
"
Q:
would expect C# to either (a) call the base class method if the derived
class is not fully constructed (which seems bad), or (b) provide a syntax
for initializing derived class members using constructor arguments before
base class constructors run (which seems good, and is what C++ does).
My two questions to you are then:
a.. what's up with this? Is there some goodness here that I'm missing out
on? Because this seems like a big old gotcha waiting to happen, calling
virtual methods before the object is constructed.
b.. any thoughts on how my tester buddy Ian can make his derived class
work without petitioning the owner of the base class to stop calling virtual
methods from the constructor?
Thanks!
Eric
A:
We (C#, .NET guidelines, FxCop) discourage calling virtual methods from
constructors. Forbidding it outright would be hard to do--even if we only
allowed constructors to call non-virtual methods, those methods could in
turn call virtuals. Also, it is occasionally useful.
C++ takes an object through a progression of type identities as a
constructor executes--basically, each constructor updates the v-table after
its call to the base constructor returns. As a result, virtual calls in a
constructor never go deeper than the constructor's level of inheritance.
This scheme provides a certain amount of protection against observing
uninitialized instance variables in virtual methods, but I don't think it is
a particularly useful or intuitive model. Indeed, the notion that an object
goes through a progression of base types until it finally reaches its true
type (and vice versa on destruction) is rather odd, and you could even argue
incorrect.
Unlike C++, C# (or, more correctly, .NET) immediately assigns a newly
allocated object its true type. Thus, virtual call dispatch is consistent
across the entire lifetime of the object, but it is possible to receive
virtual calls before execution reaches a constructor body. The big
difference between C++ and .NET is that .NET always zeroes out the memory of
a newly allocated object before executing any constructor code. So, while
virtual methods may observe the zeroed out state of instance variables, they
will never observe an undefined state and type safety is not compromised.
BTW, .NET's behavior is consistent with most other OOP systems, Java
included. In fact, I don't know of any language but C++ that uses the
"multiple personalities" approach to object construction.
Anders
"
(from http://blogs.msdn.com/scottwil/archive/2005/01/14/353177.aspx)
"
Q:
would expect C# to either (a) call the base class method if the derived
class is not fully constructed (which seems bad), or (b) provide a syntax
for initializing derived class members using constructor arguments before
base class constructors run (which seems good, and is what C++ does).
My two questions to you are then:
a.. what's up with this? Is there some goodness here that I'm missing out
on? Because this seems like a big old gotcha waiting to happen, calling
virtual methods before the object is constructed.
b.. any thoughts on how my tester buddy Ian can make his derived class
work without petitioning the owner of the base class to stop calling virtual
methods from the constructor?
Thanks!
Eric
A:
We (C#, .NET guidelines, FxCop) discourage calling virtual methods from
constructors. Forbidding it outright would be hard to do--even if we only
allowed constructors to call non-virtual methods, those methods could in
turn call virtuals. Also, it is occasionally useful.
C++ takes an object through a progression of type identities as a
constructor executes--basically, each constructor updates the v-table after
its call to the base constructor returns. As a result, virtual calls in a
constructor never go deeper than the constructor's level of inheritance.
This scheme provides a certain amount of protection against observing
uninitialized instance variables in virtual methods, but I don't think it is
a particularly useful or intuitive model. Indeed, the notion that an object
goes through a progression of base types until it finally reaches its true
type (and vice versa on destruction) is rather odd, and you could even argue
incorrect.
Unlike C++, C# (or, more correctly, .NET) immediately assigns a newly
allocated object its true type. Thus, virtual call dispatch is consistent
across the entire lifetime of the object, but it is possible to receive
virtual calls before execution reaches a constructor body. The big
difference between C++ and .NET is that .NET always zeroes out the memory of
a newly allocated object before executing any constructor code. So, while
virtual methods may observe the zeroed out state of instance variables, they
will never observe an undefined state and type safety is not compromised.
BTW, .NET's behavior is consistent with most other OOP systems, Java
included. In fact, I don't know of any language but C++ that uses the
"multiple personalities" approach to object construction.
Anders
"
Larry Lard said:Joanna said:Delphi, C#'s [spiritual] parent
[...] does allow code before inherited constructor calls.
Perhaps someone could ask Anders Heljsberg why he changed his mind about
this?
--
Larry Lard
(e-mail address removed)
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version