What is the main difference in object creation between Java and C++?

后端 未结 7 556
感动是毒
感动是毒 2021-01-31 19:12

I\'m preparing for an exam in Java and one of the questions which was on a previous exam was:\"What is the main difference in object creation between Java and C++?\"

I t

7条回答
  •  无人及你
    2021-01-31 19:56

    There is one main design difference between constructors in C++ and Java. Other differences follow from this design decision.

    The main difference is that the JVM first initializes all members to zero, before starting to execute any constructor. In C++, member initialization is part of the constructor.

    The result is that during execution of a base class constructor, in C++ the members of the derived class haven't been initialized yet! In Java, they have been zero-initialized.

    Hence the rule, which is explained in paercebal's answer, that virtual calls called from a constructor cannot descend into a derived class. Otherwise uninitialized members could be accessed.

提交回复
热议问题