Avatar ·

ما هي إيديوما copy-and-swap في C++؟

مرحباً بالجميع! أنا أتعلم C++ وأحاول فهم كيفية كتابة constructor النسخ و operator الإسناد بشكل صحيح. قرأت عن قاعدة الثلاثة، لكني كثيراً ما أجد ذكراً لإيديوما "copy-and-swap". لدي class بسيط يدير الذاكرة الديناميكية: ```cpp class String { public: String(const char* str = "") { size = strlen(str); data = new char[size + 1]; strcpy(data, str); } ~String() { delete[] data; } // Constructor النسخ String(const String& other) { size = other.size; data = new char[size + 1]; strcpy(data, other.data); } // Operator الإسناد - هنا مشكلتي String& operator=(const String& other) { // كيف أفعل ذلك بشكل صحيح؟ if (this != &other) { delete[] data; size = other.size; data = new char[size + 1]; strcpy(data, other.data); } re