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 #35

Rule of Three Violation

mediumRule of Three

This class manages dynamic memory. Which rule does it violate and what could go wrong?

📄 Code

1#include <iostream>
2using namespace std;
3 
4class Matrix {
5public:
6 double* elements;
7 int rows, cols;
8 
9 Matrix(int r, int c) : rows(r), cols(c) {
10 elements = new double[rows * cols]();
11 }
12 
13 ~Matrix() {
14 delete[] elements;
15 }
16 // No copy constructor or assignment operator defined
17};
18 
19void printFirst(Matrix m) { // pass by value
20 cout << m.elements[0] << endl;
21}