Trying to override () operator in my Custom class Queue in order to be able to copy a few element from one Queue to another, smth like that:
Queue q1(2);
Queue q2(4);
q1.enqueue(1);
q1.enqueue(2);
q2.enqueue(3);
q2.enqueue(4);
q2.enqueue(5);
q2.enqueue(6);
q2(0, 2) = q1;
cout << "q1: " << q1 << endl;
cout << "q2: " << q2 << endl;
So, expected result is when q2 is {1,2,5,6} and q1 {1,2}. I've written some code to get such result:
Queue &Queue::operator=(Queue &q)
{
Queue buffer(this->size, this->timeToServe);
for (int i = 0; i < end; i++)
{
buffer[i] = this->queuePtr[i];
}
delete[] queuePtr;
this->end = q.size;
queuePtr = new int[end];
for (int i = 0; i < end; i++)
{
queuePtr[i] = q.queuePtr[i];
}
for (int i = 0; i <= 2; i++) {
queuePtr[i] = buffer.queuePtr[i];
}
return *this;
}
//inside Queue class
Queue operator()(int b, int e) {
this->cBegin = b;
this->cEnd = e;
return *this;
}
But my Queue &Queue::operator=(Queue &q) is never executed in such usage (it works perfectly with q2 = q1). Why? Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire