← Back to Question List
C++ Quiz #33
Missing Assignment Operator
This class has a destructor and copy constructor, but the code still crashes. What is missing?
📄 Code
1#include <iostream>2#include <cstring>3using namespace std;45class MyString {6public:7 char* str;8 MyString(const char* s) {9 str = new char[strlen(s) + 1];10 strcpy(str, s);11 }12 MyString(const MyString& other) {13 str = new char[strlen(other.str) + 1];14 strcpy(str, other.str);15 }16 ~MyString() { delete[] str; }17};1819int main() {20 MyString a("hello");21 MyString b("world");22 b = a; // assignment, not copy construction23 return 0;24}