- Examples

Below, we can find some examples of copy and move semantics in action.

Copying and moving strings #

Press + to interact
#include <iostream>
#include <string>
#include <utility>
int main(){
std::string str1{"ABCDEF"};
std::string str2;
std::cout << "\n";
// initial value
std::cout << "str1 = " << str1 << std::endl;
std::cout << "str2 = " << str2 << std::endl;
// copy semantic
str2= str1;
std::cout << "str2 = str1;\n";
std::cout << "str1 = " << str1 << std::endl;
std::cout << "str2 = " << str2 << std::endl;
std::cout << "\n";
std::string str3;
// initial value
std::cout << "str1 = " << str1 << std::endl;
std::cout << "str3 = " << str3 << std::endl;
// move semantic
str3= std::move(str1);
std::cout << "str3 = std::move(str1);\n";
std::cout << "str1 = " << str1 << std::endl;
std::cout << "str3 = " << str3 << std::endl;
std::cout << "\n";
}

Explanation

  • In the example above, we are demonstrating how the value of str1 can be transferred to strings using the copy semantic and the move semantic.

  • In line 17, we have used the copy ...