Note that there are some explanatory texts on larger screens.

plurals
  1. PODifferences between the C++ and the Java object model
    primarykey
    data
    text
    <ol> <li><p>In Java it is very easy to serialize objects. In C++ it is only safe(?) to <code>memcpy</code> objects as long as they are like C structs (no polymorpism). In C++, <strong>if the compiler is able to generate the default (trivial) copy constructor then why can't it generate code for automatic serialization?</strong></p></li> <li><p><del>In Java, only static functions and data members are reachable from the ctor. In C++ I can happily use the non-static members and functions from the ctor.</del></p></li> <li><p>In Java, I can initialize data members inline, in the class. In C++ it is a compile error.</p></li> <li><p>In Java I can initialize <code>final</code> members in the ctor. In C++ I have to do the initialization of the <code>const</code> members in the initialization list. <strong>In C++, when control reaches the body of the ctor, all the members ctor has run, right?</strong></p></li> <li><p>In Java a ctor can call another ctor. In C++ we cannot do that.</p></li> <li><p>In Java, the <code>this</code> is not valid until after the ctor returns (escape of the <code>this</code> reference, a bug in multi-threading). When is <code>this</code> valid in C++? The <code>this</code> can easily escape both in C++ and in Java: <strong>registering a not yet constructed object to Listeners in the ctor</strong> (observer pattern).</p></li> <li><p>In Java, I cannot make a public function of the base class private in the derived class. I was shocked to see that in C++ is OK and even useful.</p></li> </ol> <p>Could anyone give a short explanation for these differences?</p> <p><strong>Update.</strong> Trying to collect the answers got so far.</p> <ol> <li><p>Boost has some serialization-like support. (Tony)</p></li> <li><p>Even though I messed up this point, Alf P. Steinbach gave an interesting example.</p></li> <li><p>C++0x will support much more practical initialization than C++98. (Alf P. Steinbach) #3 will be legal in C++0x (Ken Bloom)</p></li> <li><p>The data members declared in the constructor's own class are guaranteed to have been fully constructed by the time the constructor's {body} starts executing. (c++-faq-lite)</p></li> <li><p>C++0x will allow constructors to call other peer constructors (Wikipedia, C++0x)</p></li> <li><p>C++03 considers an object to be constructed when its constructor finishes executing (Wikipedia).</p></li> <li><p>Things like access control have little to do with the object model: that's a feature of the access control system which is a compile time feature. (Yttrill)</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload