Mike Wahler said:
No. (well a compiler is not disallowed from doing
it that way, but it's not required).
For user types, class x may not even have a default constructor, so in
general the compiler cannot do it this way.
'x' is not necessarily initialized prior to entering
the constructor body. The only time that would be
guaranteed to happen is if 'x' were of a user-defined
type with a default constructor (type 'int' is not).
Not exactly true on "if 'x' were of a user-defined type with a default
constructor ". 'x' may be a user defined type without a default
constructor, or any constructors, but it contains objects that have a
default constructor, so in that case there is a compiler generated default
constructor that calls the default constructor of the contained objects.
struct X {
std::string s;
};
S::S(const X& x2) { x = x2; }
// above inefficient: same as
S::S(const X& x2) : x() { x = x2; }
// which is practically same as (though the syntax is illegal in C++)
S::S(const X& x2) : x.s() { x = x2; }
Also, if you declare your class S at global or static scope you get zero
initialization, even if 'x' is a fundamental type. Also a strange rule for
historical reasons.