Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Subclass construction

15 views
Skip to first unread message

James R. Kuyper

unread,
Oct 6, 2017, 5:07:27 PM10/6/17
to
On 2017-10-06 16:30, Stefan Ram wrote:
> I saw this in a Java book:
>
> |The circular instance initialization problem is a can of
> |worms for language designers. C++ addresses the problem by
> |changing the type of the object from the superclass type to
> |the subclass type during construction.
>
> What is he talking about?

A google search for "circular instance initialization" (the double
quotes are important) gives 5 hits, three of which are different copies
of the same Java puzzle. I'll let you read that puzzle rather than
trying to summarize it.

12.6.2p10 describes the precise order in which parts of an object are
initialized. A key provision is that base class constructors get
executed before derived class constructors.

12.7p4 says "When a virtual function is called directly or indirectly
from a constructor or from a destructor, including during the
construction or destruction of the class’s non-static data members, and
the object to which the call applies is the object (call it x) under
construction or destruction, the function called is the final overrider
in the constructor’s or destructor’s class and not one overriding it in
a more-derived class."

One way to describe this is to say that a object of a derived type only
becomes an object of the derived type after all of it's base class
constructors have completed.
Those two rules combined prevent the problem described in that puzzle
from being a problem in C++. At least, that's my understanding. Being
used to those rules, and not being particularly familiar with Java, I
found the description of that puzzle somewhat confusing.
0 new messages