Everyone says that the Ctor movement is a shallow copy or just a pointer assignment.
Not everyone says that this is not so. A move constructor is what you define it. In your example, your ctor movement does something completely different. The ctor move point — unlike the ctor copy — is that you know that another object is about to be destroyed, so you can cannibalize or move its resources.
The move constructor can make a “shallow copy”, although this term is colloquial and not clearly defined in C ++. "Just a pointer assignment" - perhaps, perhaps, sometimes.
But how can I initiate my object with a pointer to a temporary object (when this object is destroyed, my object will point to an unknown address, and this is not valid from my point of view).
You do not (usually) initialize an object of type T pointer of type T* , as such. You can assign my_t = *my_t_ptr , or if you know you cannibalize the T pointed to by my_t_ptr since it will be deleted soon, then you can assign my_t = std::move(*my_t_ptr) .
Can you give me a more suitable example [of a significant difference between the move constructor and the copy constructor] ...?
A “classic” example is when your T is created by allocating some space on the heap. When you copy-build one T from another, you have no choice but to allocate a second piece of memory; when you move-build a T , you can: 1. copy the pointer from the existing T to T 2. Set the existing T element pointer to nullptr . In this case, you always use only the stock space T
source share