In Java it is very easy to serialize objects. In C++ it is only safe(?) to
memcpyobjects as long as they are like C structs (no polymorpism). In C++, if the compiler is able to generate the default (trivial) copy constructor then why can't it generate code for automatic serialization?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.In Java, I can initialize data members inline, in the class. In C++ it is a compile error.
In Java I can initialize
finalmembers in the ctor. In C++ I have to do the initialization of theconstmembers in the initialization list. In C++, when control reaches the body of the ctor, all the members ctor has run, right?In Java a ctor can call another ctor. In C++ we cannot do that.
In Java, the
thisis not valid until after the ctor returns (escape of thethisreference, a bug in multi-threading). When isthisvalid in C++? Thethiscan easily escape both in C++ and in Java: registering a not yet constructed object to Listeners in the ctor (observer pattern).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.
Could anyone give a short explanation for these differences?
Update. Trying to collect the answers got so far.
Boost has some serialization-like support. (Tony)
Even though I messed up this point, Alf P. Steinbach gave an interesting example.
C++0x will support much more practical initialization than C++98. (Alf P. Steinbach) #3 will be legal in C++0x (Ken Bloom)
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)
C++0x will allow constructors to call other peer constructors (Wikipedia, C++0x)
C++03 considers an object to be constructed when its constructor finishes executing (Wikipedia).
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)
memcpythem then. (Also, a ctor can call another ctor in C++0x).std::set_symmetric_differenceorstd::lexicographical_compare). It's more common to see shortened names in C because C specifies that linker names are only required to be differentiated by the first 8 characters.thisreference to escape to another Thread inside the constructor. Of course, allowing thethisreference to escape is rather inadvisable.