C++: Copy a String


This task is about copying a string. Where it is relevant, distinguish between copying the contents of a string versus making an additional reference to an existing string.

#include <iostream>
#include <string>

int main( ) {
	std::string original ("This is the original");
	std::string my_copy = original;
	std::cout << "This is the copy: " << my_copy << std::endl;
	original = "Now we change the original! ";
	std::cout << "my_copy still is " << my_copy << std::endl;


Content is available under GNU Free Documentation License 1.2.

Subscribe to TFE Times

Enter your email address to become a member of TFE Times today!