+ 1

Is this code fine? C++ topic

I just solved C++ "Queue Management Part 2" by adding this code in the //code goes here or something: Queue operator+ (Queue &test) { Queue result; for(int i = 0; i < this->size; i++) { result.queue[result.size++] = (this->queue[i]); } for(int i = 0; i < test.size; i++) { result.queue[result.size++] = (test.queue[i]); } return result; } is what I did fine? or is there any easier way to do it?

5th Dec 2021, 1:32 PM
A Regular Name
1 Réponse
+ 1
It would be better to call the existing add() method rather than re-implementing it twice. Keep it in one place for easier future maintenance. Queue operator+(Queue &test) { Queue result; for (int i = 0; i < this->size; i++) { result.add(this->queue[i]); } for (int i = 0; i < test.size; i++) { result.add(test.queue[i]); } return result; }
6th Dec 2021, 6:52 PM
Brian
Brian - avatar