CJCoding With Joseph
15per day
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
C++ Quiz #33

Missing Assignment Operator

mediumRule of Three

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;
4 
5class 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};
18 
19int main() {
20 MyString a("hello");
21 MyString b("world");
22 b = a; // assignment, not copy construction
23 return 0;
24}