4

My problem is to understand how exactly these two constructors work. I have this class:

class moveClass
{
  int variabile;
  public:
  moveClass(){} 
      //move constructor
  moveClass(moveClass && arg)
  {
    cout<<"Call move constructor"<<endl;
    variabile = arg.variabile;
  } 
      //copy constructor
  moveClass(const moveClass & arg)
  {
    cout<<"Call copy constructor"<<endl;
    variabile = arg.variabile;
  }  
};

From what i understand, when i instance a new object of this class, a constructor is called based on the type of the parameter.

The advantage of the move constructor is that when an rvalue is used to instance an object, that object isn't copied, but just moved.

1 moveClass a;
2 moveClass b = a;
3 moveClass c = std::move(a);

Considering this example, can i say that when i instance b, a is copied, then assigned to b?

In other words up until line 2 i will have 3 objects in memory: a, b and a_copy.

While line 3 will just create the c object and no new copy objects.

Basically at the end of these three lines i will have in memory 4 objects. Is this correct?

The code of the constructors is also the same, so i expect that the only difference is the type of the argument passed.

Neuron
  • 5,141
  • 5
  • 38
  • 59
  • 1
    Using an `int` as the moved member defeats the point, as that cannot be moved, so the generated code will just copy it, and no difference will be seen or performance will be gained. – underscore_d Apr 13 '18 at 13:48

1 Answers1

2

In other words up until line 2 i will have 3 objects in memory: a, b and a_copy.

No.

moveClass b = a;

is the same as

moveClass b(a);

So, the copy constructor for b is called and you directly copy the members from a into b, no temporary(copy) is generated.

At then end of it all you have only constructed 3 objects, a, b and c.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Hi, thank you for the reply. So what is the point of using move constructors? Will i see a difference only if i have some pointers in my class? – Domenico Paolicelli Apr 13 '18 at 13:51
  • @DomenicoPaolicelli See this: https://stackoverflow.com/questions/3106110/what-are-move-semantics. – NathanOliver Apr 13 '18 at 13:54
  • Hi Nathan, i already read that. I can see how he makes point the data to a null pointer. So the move constructor is something i can see only with a pointer? or just that variable will just not be initialized in the third line? – Domenico Paolicelli Apr 13 '18 at 13:56
  • 1
    @DomenicoPaolicelli The point of a move constructor is to not have to copy a temporary. If you have `SomeBigClass foo = MakeSomeBigClass()` instead of having to copy that very large class you can just move it into `foo`. Generally this only works with classes that use pointers internally as as moving a POD type is the same as copying it. – NathanOliver Apr 13 '18 at 13:59
  • Another good example is a `std::vector`. If it has a 1000 elements you would have to copy all 1000 elements. Moving just swaps the internal pointers, so it is a lot faster. – NathanOliver Apr 13 '18 at 14:00
  • Basically i can only see a difference when i use a pointer, and the difference is that with a pointer i just make the object of the class point to the object of the temporary class without the need to create a new pointer. Right? – Domenico Paolicelli Apr 13 '18 at 14:02
  • @DomenicoPaolicelli Yes, you avoid another dynamic allocation and you avoid copying the data. – NathanOliver Apr 13 '18 at 14:03
  • Oh great! I finally understood! Really thank you Nathan :) – Domenico Paolicelli Apr 13 '18 at 14:03