What is the copy-and-swap idiom in C++?
Hello everyone! I'm learning C++ and trying to understand the correct way to write a copy constructor and assignment operator. I've read about the rule of three, but I often come across mentions of some "copy-and-swap" idiom.
I have a simple class that manages dynamic memory:
```cpp
class String {
public:
String(const char* str = "") {
size = strlen(str);
data = new char[size + 1];
strcpy(data, str);
}
~String() { delete[] data; }
// Copy constructor
String(const String& other) {
size = other.size;
data = new char[size + 1];
strcpy(data, other.data);
}
// Assignment operator - this is where I have problems
String& operator=(const String& other) {
// How to pr