E
Eric Sosman
Is there any case where the C compiler rejects casts? I cannot imagine an
example.
(struct tm)42;
Is there any case where the C compiler rejects casts? I cannot imagine an
example.
Andreas said:Lew said:You asked if there were specific cases where it was good to use an
overridable (you never said 'final' [...])
But 'final' surely implies non-overridability, doesn't it? Therefore,
overridability of a method implies its non-'final'ity.
Tom said:It really doesn't. The problem with calling overridable methods from
constructors is that they can be overridden with code which assumes a
constructed object, and gets into trouble when run in the context of one which
isn't. getClass() can't get into that kind of trouble.
Jim said:Lew writes:
Consider focussing on what I did say, instead of what it sort of sounds
like I might have said. The solution is in the paragraph above, the one
that starts with "In effect, until the constructor of Foo completes".
In the message I was replying to, in the text that you deleted, Owen
Jacobsen correctly observed that
This is a technical issue, and I observed that other languages have
found ways to prevent virtual methods from being called before their
owning objects have been fully constructed. One can debate whether this
desirable, but C++ provides an existence proof that it's possible.
Daniele Futtorovic said:class BaseClass implements [...] {
Daniele Futtorovic said:class BaseClass implements [...] {
In principle this is the same kind of example as Tom's.
Could it have been designed differently? Sure. Java's constructor
semantics are a weird-but-mostly-intuitive mix of C++'s constructors and
Smalltalk's initializers-are-just-methods approach (where you're not
forced to chain to a parent class's initializer at all).
Tom Anderson said:No. getClass() is final, not virtual, it is never overridden, and it does
not behave like it has been overridden. Rather, it behaves like its
implementation was:
public Class getClass() {
return this.class;
}
Where all objects have a secret magic hidden field called 'class' which is
set by the VM when they are allocated.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.