[Cs3] Move Constructor Definition

Mikhail Nesterenko mikhail at cs.kent.edu
Mon Oct 9 10:13:09 EDT 2023


Good question. So, there is no standard way to code a move
constructor. However, the idea is that you could use the resources of
the original object and destroy it in the process. For example, have
the pointer of the copy-object to point to the buffer of the orignial.

Thanks,
--
Mikhail

> 
> I was reviewing the definition for a move constructor and found two examples; one in a lab we completed and the other in the examples on your website. The definitions were different, and I wanted to clarify which one was correct, or if both were ok.
> 
> The one from the example on your site was:
> 
> MovableInt(MovableInt&& org) noexcept {
> ??????????????????size_ = org.size;
> ??????????????????org.size_ = 0;
> ??????????????????array_ = org.array_;
> ??????????????????org.array = nullptr;
> }
> 
> The move constructor from the lab was implemented this way:
> 
> Student(Student&& org) noexcept :
> ??????????????????firstName_(move(org.firstName_)),
> ??????????????????lastName_(move(org.lastName_))
> {}
> 
> I also noticed that the second example is using an initializer list. Could the first example be coded using an initializer list as well, like this:
> 
> MovableInt(MovableInt&& org) noexcept :
> ??????????????????size_(move(org.size_)),
> ??????????????????array_(move(org.array_))
> {}
> 
> Thank you,
> 
> Josh Romisher



More information about the cs3 mailing list