0

Why is queue a pointer variable in Queue Management assignment (parts 1 and 2) (C++)

I have finished Queue Management parts 1 and 2 in the C++ course, but I am treating q.queue and q.size the same way I would do if I didn't know about pointers. The class Queue contains: class Queue { int size; int* queue; (...) And I solved Part 2 (operator method) as below. I have called both size and queue with the "this" element. So what is the difference between the two variables? One is a pointer, so it should be treated differently somehow? Or, as I suspect, when we access q.queue[i], we are implicitly using the i-th element of the array by reference? Queue operator+(Queue q2) { // Create Queue object to return Queue q3; // Fill q3 from q1 info q3.size = this->size; q3.queue = this->queue; // Fill the rest of the queue with the second object for (int i = 0; i < q2.size; i++) { q3.queue[q3.size] = q2.queue[i]; q3.size++; } // Output of function return q3; }

30th Mar 2022, 5:59 AM
Paek Se
Paek Se - avatar
1 Respuesta
+ 1
The <queue> member is defined as int pointer because it will be initialized dynamically in constructor as data storage (with 100 free slots), using dynamic memory allocation approach. `this -> size` and `this -> queue` refer to the <size> and <queue> member in the scope of current object, the object on which the overloaded operator was called upon.
30th Mar 2022, 6:45 AM
Ipang