0

I have created the following class:

class CList
{
private:
    Cust *custArray{new Cust [1]}; 
    size_t arrayCap{1};                  
    size_t used{0};                       
    // also, the number of used cells
public:
    CList() = default;
    CList(CList&&);  

class Cust
{

private:
    const char *name;
    const char *email;
    size_t id;

public:
    Cust()
    {
    }
    Cust(const char *_name, const char *_email, size_t _id)
    {
        name = _name;
        email = _email;
        id = _id;
    }
};

I need help in implementing CList(CList&&) move constructor. Will it simply create a copy of the object that is passed?

3
  • Move constructors, like it says, don't copy anything. They move values around, leaving the passed object in an unspecified state. Commented Aug 15, 2021 at 13:30
  • 1
    CList(CList&& x) : custArray{std::exchange(x.custArray, nullptr}, arrayCap{std::exchange(x.arrayCap, 0)}, used{std::exchange(x.used, 0)} { } Commented Aug 15, 2021 at 13:31
  • 1
    Don't do manual memory allocation if you don't have to. If you use a vector, you could have: class CList { private: std::vector<Cust> custArray; public: CList() = default; CList(CList&&) = default; }; Commented Aug 15, 2021 at 13:36

1 Answer 1

1

A move constructor typically transfers resources to the new object, rather than copy them. Such transfer normally involves copying just the handle (a pointer, a file descriptor, or whatever) rather than whatever resource the handle references. Once this is done, the constructor makes sure that the moved-from object does not reference any resources (i.e. the handles are null or invalid or whatever). For example:

CList(CList&& other) {
   custArray = other.custArray; // copy the handle
   arrayCap = other.arrayCap;   // copy supporting information
   used = other.used;           // copy supporting information
   other.custArray = nullptr;   // zero out the old handle
}

You probably should use std::exchange in real code. More importantly though, in real code you should use std::vector instead of a manually-allocated array, which makes the hand-written move constructor unnecessary altogether.

Sign up to request clarification or add additional context in comments.

6 Comments

Can if (this == &other) return; ever happen? The object is just now under construction, meaning it cant be used to move to itself unless you write CList foo = std::move(foo);
This seems exactly the same as overloading the assignment operator operator=. Is there any difference between these two?
@joseph This seems exactly the same as overloading the assignment operator operator= No, these things are very different.
@joseph There are significant differences. Constructors create objects that did not exist before. Assignment is changing the state of an existing object, which may have to clean up old state first before taking the new state. Assignment typically returns a reference to self as well. Between move-ctor and move-assignment operators, what happens with the moved object is about the same, however.
Writing a move constructor and also a move assignment operator are usually done together. If one exists, the other is probably desired too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.