By default, the C++ compiler generates a copy constructor for every class. This default copy constructor copies all members (using their copy constructors, of course). For example, in a class

class Z {
    X x;
    Y y;
    // define stuff, but don't define Z Z(const Z& z)
    // ...
which does not define any copy constructor, the compiler supplies a default copy constructor which behaves like
Z::Z(const Z& z) : x(z.x), y(z.y)
  // nothing else to do
This would call the copy constructor X::X(const X&) and Y::Y(const Y&). This seemingly infinite nesting of default copy constructors is interrupted when a primitive type (e.g. char or double) is copied: these semantics are hard-coded into the language.

Note that the default copy constructor's semantics extend those of copying structs in C.

Log in or register to write something here or to contact authors.