move constructor

closed account (jGAShbRD)
template <typename Type>
my_unique_ptr<Type>::my_unique_ptr(my_unique_ptr<Type> && object)
////Move constructor that takes my_unique_ptr object and constructs a new object from it.
{
ptr = object.ptr;
object.ptr = nullptr;
}
i wrote my move constructor like this. in my private class i have Type * ptr;
i want to know what this does and each line does. like is the first line setting the ptr to point to what the object is pointing to and then setting that object to nothing? im confused
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) The purpose of the move constructor is that the contents of the other object - the thing you've called object in your code - are moved into this, leaving object in a state that it's no longer usable. So:

a) this->ptr is set to point to the same thing as object.ptr, so that this is now owning the same data as object

b) object.ptr is set to nullptr, so that object no longer owns the data it used to own, leaving this as the sole owner of the data.

3)
i wrote my move constructor like this. [...]
i want to know what this does and each line does.

How could you not know what the code does, if you wrote it? How could you have written those lines without knowing what they do?

Do you mean "someone else wrote a move constructor like this"?
Last edited on
closed account (jGAShbRD)
my professor sample code on move constructor was something like this
Topic archived. No new replies allowed.