Example6( Example6&& x );
This constructor takes parameter
x
that is a rvalue reference to type
Example6
object.
The x is Example6 object.
Class Example6 has member
ptr
. We access member 'ptr' of object x with syntax
x.ptr
.
Example of copy construction:
1 2 3 4 5 6 7
|
class T {
U val;
public:
T( const T& x )
: val ( x.val )
{}
};
| |
The member 'val' of constructed object is initialized with value of same member in object 'x', whose copy the constructed object will be.
1 2 3 4 5
|
Example6::Example6( Example6&& x )
: ptr(x.ptr)
{
x.ptr = nullptr;
}
| |
An Example6 object points to a std::string object.
We move content of 'x' into new object.
After construction the new object points to the string object.
The other object (that we refer to as 'x') does still exists, but it cannot point to the string because we did move the string (ownership) to the new object.
We set the pointer to nullptr to clearly show that the pointer no longer points to any object.
What does expression
foo + bar
return?