#Move constructor with pointers
30 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
what do you mean "using a move constructor"?
maybe?
probably it's just moving the pointer, and not what it is pointing at
but I'm not clear of the situation you are talking about so 
something like this:
MyClass obj2 = std::move(obj1);
With the move constructor:
MyClass(MyClass&& other) noexcept : ptr(other.ptr) {
other.ptr = nullptr;
}```
I believe pointer is getting moved, but thr issue is i believe you need to do std::exchange for it to set other to null properly
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
Like
T(T &&other): val{std::exchange(other.val, nullptr)}
ty for heads up
what do you mean "move the pointer with the pointer address"
a pointer is the address
you don't need std::exchange in this case
it's equivalent to setting it to nullptr later
Destruction will happen tho
So if you have a dtor it'll free it possibly
a pointer has no destructor
in your move constructor you need to copy the other's pointer and then set it to null
The class has
Which std::exchange does
how is this relevant to the move constructor tho?
yes it does, but it's not needed to do it that way, is what I'm saying
... ptr(other.ptr) {
other.ptr = nullptr;
}```
does the same thing