Misunderstanding here:
*this means that it passes the address of any cloned object
Actually, this is the address of the object being cloned, but *this (note the asterisk) is the result of dereferencing this address. So *this is of type Derived & , this is a reference to the cloned object, not its address.
Therefore, calling new Derived(*this) means that after dynamically allocating space (which makes new ), the new space is initialized by the Derived(const Derived &) copy constructor Derived(const Derived &) , which in this case was not actually defined by the user, therefore, the version of the copy constructor (generated by compiler) by default.
To clarify the semantics of new : if C is a class, then
new C;
allocates enough space for an object of type C , and then calls the constructor of C to initialize this space. This is part of the new semantics: it always calls the constructor to initialize the newly allocated space.
When you call
new C(a,b,c);
with some arguments a , b and C , then new calls the constructor of C , which takes these three arguments. If such a constructor is not defined, you will get a compiler error.
Now in the special case when you call
new C(a);
with argument a , which in itself is a C& type, new , as always, will invoke the corresponding constructor. The corresponding constructor is either C(C &) (if defined) or C(const C&) (copy constructor automatically determined by the compiler).
source share