Click here to Skip to main content
15,878,003 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, actually am trying to understand about the move semantics the thing is am not getting the theory in practically. Only one doubt is how move semantics moves bcz in move constructor am using move function, the theory says that transfer the ownership to another resource means copy constructor will call right. I don't know how move Constructor working anyway it needs to copy right. So, help me how move semantics works and information of address of object after the move constructor pls describe in-depth.

What I have tried:

Tried to understand about the move semantics nut having some doubts.
Posted

In order to understand the move semantics, first and foremost you must understand what a temporary object is. Which is, any object created on the fly and then is immideately discarded:

C++
class A{/*assume it has functionality*/};

A a; // Solid object
A a1 = A(/* constructor parameter(s)*/); // Where as a1 is solid, the RValue assignment is a temporary objest which is when discarded triggers destructor semanticts


So move semantics says Move A() into a1, no copying like having 2 object temprarily occupying memory.

That can work even without the move semantics, given that proper copy constructor and assignmens are implemented. But the move semantics eliminates redundant copies, say if the object is huge in size.
 
Share this answer
 
v9
I cover move semantics in one of my articles

[^]

Move semantics basically 'move' the internal state of an object into another object in the right circumstances, thereby avoiding the need for creating a new object and copying its state.

It is the compiler which will determine this during compilation. It looks at your assignment and based on whether it deems certain variable to be 'temporary', it can 'move' the internal state of the temporary variable into your target value without having to go through a copy constructor.

Note however that this is not always safe. Implemented wrongly, this can lead to memory leaks if the target object does not properly discard its internal resources before they are overwritten.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900