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.

1
  • 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. Commented Apr 13, 2018 at 13:48

1 Answer 1

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.

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

8 Comments

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?
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?
@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.
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.
|

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.