mercredi 16 septembre 2020

Is there a way to move a pointer from a queue to another?

I need to take a pointer to an object stored in one queue, process it, and move to another queue that is in another object. Following, I'll show the structure (schematically) of the problem.

class A{
  public:
    A();
    ...
    // fields and methods
}

class B : public A{
  public:
    B();
    ...
    // extra fields and methods (not in A)
    
}
class C : public A{
  public:
    C();
    ...
    // extra fields and methods (not in A)
    
}


class Sender{
  public:
    Sender();
    std::queue<A*> qSender;
    ...
    // fields and methods
}

class Receiver{
  public:
    Receiver();
    std::queue<A*> qReceiver;
    void moveToReceiver(Sender& s, int n);
    ...
    // fields and methods
}

I've used the queue with pointer to base class to overcome the slicing object problem, but I don't know if is the best way to do. I've filled the qSender queue following the

qSender.push_back(new B(...)));

Then i've tried to move from a qSender to qReveiver with this code:

void moveToReceiver(Sender& s, int n){
  for(int i = 0; i < n; i++){
    A* a = senderObject.qSender.front();
    senderObject.qSender.pop();
    qReceiver.push(a);
  }
}

But I've noticed that the elements are not being pop; the code is only "moving" n pointers to the first object to the qReceiver and not removing from qSender.

Any ideas to solve that are welcome.

Aucun commentaire:

Enregistrer un commentaire