new vs override - a practical example

  • Thread starter Thread starter bonk
  • Start date Start date
You are casting test to A (at least statically) when you assign it to
an A variable.

Jesse
 
You are casting test to A (at least statically) when you assign it to
an A variable.

I realize that "without a cast" is a bit ambiguous, I would usually
interpret it as without using the cast operator -- which is the issue here.

< splitting hairs >
Actually I can't find your definition of a cast in ISO/IEC
23270:2003(E). I can find a definition of "implicit conversions" which
captures the above:

* From any class-type S to any class-type T, provided S is derived from T.
* From any class-type S to any interface-type T, provided S implements T.
* From any interface-type S to any interface-type T, provided S is
derived from T.

Although there are a few examples which hints to (unqualified) "cast"
being used the way you specify.
</ splitting hairs >

Claiming that the need to implicitly convert (cast) from a type to
another will help the programmer in any way when using "new" is void.
Implicit conversion is exactly implicit.

In many cases it is not even clear from the immediate context whether an
implicit conversion occurs. Further more, changing a declaration, may
change a situation without implicit conversion to one without, or
vice-versa, and change the semantics of the program -- without so much
as a warning.

BTW: This is also why overloading with sub-classing is so error-prone.
 
Back
Top