0

I have vector of pointers in my class:

std::vector<Customer *> customers

Now I want to implement the move constructor. I'v find out that I can use std::move of std::vector. the problem is that I don't know if it will clear the old vector values. please if anyone can explain it to me.

my move constructor:

OpenTable::OpenTable(OpenTable&& other) : BaseAction(), tableId(other.tableId)
{
    customers=std::move(other.customers);
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
Sprint21
  • 89
  • 6

2 Answers2

3

Your move constructor will do what you want, no need to clear anything

After std::move()'ing from an std::vector, the old vector will have 0 elements and no dynamic memory allocated for it. The newly-constructed vector takes that memory away. No need to clear any elements.

However, it should also be said that:

I have vector of pointers in my class:

This is a mistake... we're not using C++98 anymore. Raw pointers do not indicate who owns the memory, nor the lifespan of the object at the address. Now, you may get it right, but you may not. And what about a future maintainer of your code? Better not to leave it to chance: Use a smart pointer instead. Or - just place the actual objects in the vector. As comments suggest, if you're moving rather than copying the vector, you won't be making extra copies of the same objects.

More on this point in the Resource Management section of the C++ Core Programming Guidelines.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    Or not a pointer at all. If you are moving them around, why not store a vector of objects. – Fantastic Mr Fox Nov 13 '18 at 15:09
  • 1
    @user2079303 If they own a resource, they should be RAII types. Can you think of a way in this specific case where this wouldn't be appropriate? – Fantastic Mr Fox Nov 13 '18 at 15:11
  • 2
    Important part **If they own a resource**. How do you know that those pointers own a resource? – eerorika Nov 13 '18 at 15:12
  • I guess it is true, however, I cannot use smart pointers in my homework. thank you – Sprint21 Nov 13 '18 at 15:12
  • 1
    @user2079303 They are stored in a vector in a class. If they aren't owning (or sharing ownership) then you have a strong link between objects with unclear object lifetimes. Although technically you are correct and there is not enough info to say we absolutley should use a `smart_ptr` i would hazard a guess that we probably should. – Fantastic Mr Fox Nov 13 '18 at 15:15
  • In my customer object there are no dynamic resources – Sprint21 Nov 13 '18 at 15:16
  • 1
    @Sprint21 That's not relevant in this context. What is relevant is whether those customer objects themselves are dynamic resources (i.e. are they allocated dynamically). – eerorika Nov 13 '18 at 15:17
  • in that case, you right, they are allocating dynamically – Sprint21 Nov 13 '18 at 15:41
2

the problam is that I don't know if it will clear the old vector values.

Vector is guaranteed to be empty after it has been moved from.

customers=std::move(other.customers);

Instead of default constructing the member and then move-assigning it, it is better to move-construct the member directly in the member initialization list:

OpenTable::OpenTable(OpenTable&& other) : ..., customers(std::move(other.customers))

Although, it looks a lot like your constructor doesn't do anything different from the implicit move constructor, so you might use instead:

OpenTable::OpenTable(OpenTable&&) = default;

Even better, depending on the rest of your class, the move constructor may be implicitly declared, so you might not even need that defaulted declaration.

eerorika
  • 232,697
  • 12
  • 197
  • 326