#Move constructor with pointers

30 messages · Page 1 of 1 (latest)

round quest
#

when using a move constructor with a pointer member, is it moving the entire pointer itself along with pointer address, or just moving the the address of what the pointer is pointing to?

peak wingBOT
#

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.

craggy siren
#

what do you mean "using a move constructor"?

#

sendcode 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 itdepends

round quest
#

something like this:

MyClass obj2 = std::move(obj1);

With the move constructor:

MyClass(MyClass&& other) noexcept : ptr(other.ptr) {
        other.ptr = nullptr; 
    }```
serene onyx
#

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

round quest
#

i figured it out ty guys

#

!solved

peak wingBOT
#

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

serene onyx
#

Like

T(T &&other): val{std::exchange(other.val, nullptr)}
round quest
#

ty for heads up

white basin
#

what do you mean "move the pointer with the pointer address"

#

a pointer is the address

white basin
#

it's equivalent to setting it to nullptr later

serene onyx
#

So if you have a dtor it'll free it possibly

white basin
#

a pointer has no destructor

#

in your move constructor you need to copy the other's pointer and then set it to null

serene onyx
white basin
#

how is this relevant to the move constructor tho?

white basin
#
... ptr(other.ptr) {
    other.ptr = nullptr;
}```
#

does the same thing

serene onyx
#

Sure but its just like why

#

Already have something which does it

#

In one line