Take a look at the addition operator + as implemented in demolist12_4. Notice that it actually creates a copy and returns it. While making it easy to concatenate the strings, the addition operator + can cause performance problems. But it's solved by using move constructors and move assignment operators.
declaring a move constructor and move assignment operator
Notice that copy constructor is called in line 21, simultaneously, proceeding copy from str to SayHello.
C++11 compliant compilers ensure that for rvalue temporaries the move constructor is used instead of the copy constructor and the move assignment operator is invoked instead of the copy assignment operator.
e.g. class with move constructor and move assignment operator in addition to copy constructor and copy assignment operator. demolist12_11
MyString HelloAgain("overwrite this"); HelloAgain = Hello + World + Cpp;
return0; }
commented line 26-48 the output is:
1 2 3 4 5 6 7 8 9
constructor called hello constructor called world constructor called C++ constructor called overwrite this operator + called default constructor operator + called default constructor copy assignment operator hello world C++
whereas uncommented it:
1 2 3 4 5 6 7 8 9
constructor called hello constructor called world constructor called C++ constructor called overwrite this operator + called default constructor operator + called default constructor move assignment operator move hello world C++
The move constructor save a good amount of processing time in reducing unwanted memory allocations and copy steps. Programming the move constructor and the move assignment operator is completely optional . Unlike the copy constructor and the copy assignment operator, the compiler does not add a default implementation.
User Defined literals
c++ 11 extend the standard's support of literals by allowing you define your own literals.
To define your own literals, you define operator "" like this:
1 2 3 4
ReturnType operator""YourLiteral(ValueType value) { //conversion code here }
e.g. demonstrates a user defined literal that convert types. conversion from fahrenheit and centigrade to kelvin scale. demolist12_12